In your NSSliderCell subclass, you also need to override knobRectFlipped:
. Here's some code of mine, where mNormalKnob
is an NSImage
loaded into a member variable in initWithCoder:
.
- (NSRect)knobRectFlipped:(BOOL)flipped
{
NSSlider* theSlider = (NSSlider*) [self controlView];
NSRect myBounds = [theSlider bounds];
NSSize knobSize = [mNormalKnob size];
float travelLength = myBounds.size.width - knobSize.width;
double valueFrac = ([theSlider doubleValue] - [theSlider minValue]) /
([theSlider maxValue] - [theSlider minValue]);
float knobLeft = roundf( valueFrac * travelLength );
float knobMinY = roundf( myBounds.origin.y +
0.5f * (myBounds.size.height - knobSize.height) );
NSRect knobRect = NSMakeRect( knobLeft, knobMinY,
knobSize.width, knobSize.height );
return knobRect;
}
I guess I'm assuming that the height of the knob image is no more than the height of the view frame.
Here's another approach that ought to work without regard to the view frame height, though I haven't tried it. Designate the slider and its superview as having Core Animation layers. Make a CALayer as a member variable of your subclass, and set its content to your knob image. Then when it's time to draw the knob, just adjust the position of the knob layer.