objective-cmacoscocoansscrollviewnsscroller

How to disable the mouse hover expand effect on a NSTextView's NSScrollView scroller?


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


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];
        }
    }