Question

I have a NSTextView and here's the normal size of the scroller:
enter image description here
And here's what happens when I hover the scroller of the textview:
enter image description here
However, I don't want to have this 'expand' effect. How can I remove it? I've tried to search around on how to perform this, but I couldn't find anything. I just want to have the regular scroller size (the thinner one) all the time, even if the user hovers it. Is this possible?
Thanks

Was it helpful?

Solution

I recommend subclassing the NSScroller and override – drawArrow:highlight: / – drawKnobSlotInRect:highlight: / – drawKnob methods so you have a stable scroller appearance.

P.S. Don't forget to set your new scroller class in XIB-file for the scrollers.

UPDATE

Here is the sample code:

- (void)drawKnob
{
    // call the default implementation for Overlay Scrollers
    if (self.scrollerStyle == NSScrollerStyleOverlay)
    {
        [super drawKnob];
        return;
    }

    if (_style == NSScrollerKnobStyleLight || _style == NSScrollerKnobStyleDefault)
            [[NSColor colorWithCalibratedWhite:1.0 alpha:0.8] setFill];
    else [[NSColor colorWithCalibratedWhite:0 alpha:0.4] setFill];

    // Note: you can specify the rect with fixed width here
    NSRect knobRect = [self rectForPart:NSScrollerKnob];

    // VERTICAL SCROLLER
    NSInteger fullWidth = knobRect.size.width;
    knobRect.size.width = round(knobRect.size.width/2);
    knobRect.origin.x += (NSInteger)((fullWidth - knobRect.size.width)/2);

    // draw...
    NSBezierPath * thePath = [NSBezierPath bezierPath];

    [thePath appendBezierPathWithRoundedRect:knobRect xRadius:4 yRadius:4];
    [thePath fill];
}

//---------------------------------------------------------------

- (void)drawKnobSlotInRect:(NSRect)slotRect highlight:(BOOL)flag
{
    // call the default implementation for Overlay Scrollers
    // draw nothing for usual
    if (self.scrollerStyle == NSScrollerStyleOverlay)
    {
        [super drawKnobSlotInRect:slotRect highlight:flag];
    }
}

//---------------------------------------------------------------

- (void)drawArrow:(NSScrollerArrow)whichArrow highlight:(BOOL)flag
{   
    // call the default implementation for Overlay Scrollers
    // draw nothing for usual
    if (self.scrollerStyle == NSScrollerStyleOverlay)
    {
        [super drawArrow:whichArrow highlight:flag];
    }
}

OTHER TIPS

I don't know what exact style you want, but this category might help you.

@implementation NSScrollView (SetScrollStyle)

- (void) setHidingScroll
{
    [self setScrollerStyle:NSScrollerStyleOverlay];
    [[self verticalScroller] setControlSize: NSSmallControlSize];
    [[self verticalScroller] setKnobStyle:NSScrollerKnobStyleDark];
    [self setScrollerKnobStyle:NSScrollerKnobStyleDark];
    [[self verticalScroller] setScrollerStyle:NSScrollerStyleOverlay];
}

and usage

[scrollView setHidingScroll];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top