Home > Net >  I want to give the thumb image to NSSlider in swift
I want to give the thumb image to NSSlider in swift

Time:10-17

I`m working on an editor app, where I need to add image in the slider thumb as I'm doing macOS development so unfortunately I'm not able to do that in macOS, there is available in UISlider.

enter image description here

this is what I have and I want to insert the thumb Image as give below

enter image description here

CodePudding user response:

I find out the solution, I hope this will help you all.

I made a NSSliderCell class and override the function drawKnob.

class CustomSliderCell: NSSliderCell {

@IBInspectable var knobImage: NSImage?{
    didSet{
        if let knobImage = knobImage{
            self.knobImage = knobImage
        }
        
    }}

override func drawKnob(_ knobRect: NSRect) {
    
        var rect = knobRect
        
        rect.size = NSSize(width: rect.width, height: rect.height - 4)
        self.knobImage?.draw(in: rect)
    
        
}

}

just assign this class to you nsslider cell from your storyboard and change the image on your storyboard. enter image description here

The Result is given Below : [This is the resultant Image][2]

enter image description here

  • Related