How can I make my NSScroller subclass a different width?

I am trying to create my own NSScroller subclass. I created a class and set it as a vertical scroller on an NSScrollView in IB. When I run the project, the method drawRect:

is called for my subclass, so I know it is connected correctly.

Now, how do I change the width of my new NSScroller? No matter what I do with its borders and border, it always wants to draw a 15px wide rectangle (the size of a standard NSScroller).

+1


source to share


2 answers


In your NSScroller subclass, you need to override scrollerWidth:

+(CGFloat)scrollerWidth
{
    return 30.0;
}

      



This is the value that NSScrollView uses to define the frame for your component when configuring it.

+2


source


You can use categories to override the scroll width method for all NSScrollers.

Eg. In NSScroller-MyScroller.h:

#import <Cocoa/Cocoa.h>

@interface NSScroller (MyScroller)

+ (CGFloat)scrollerWidth;
+ (CGFloat)scrollerWidthForControlSize: (NSControlSize)controlSize;

@end

      



In NSScroller-MyScroller.m:

#import "NSScroller-MyScroller.h"
#define SCROLLER_WIDTH 30.0

@implementation NSScroller (MyScroller)

+ (CGFloat)scrollerWidth {
    return SCROLLER_WIDTH;
}

+ (CGFloat)scrollerWidthForControlSize: (NSControlSize)controlSize {
    return SCROLLER_WIDTH;
}

@end

      

+1


source







All Articles