tags:

views:

515

answers:

2

I am trying to create a custom subclass of NSScroller. I have created the class, and set it as the vertical scroller on an NSScrollView in IB. When I run my project, the drawRect: method is called for my subclass, so I know that it is properly connected.

Now, How do I change the width of my fancy new NSScroller? No matter what I do to its bounds and frame, it always wants to draw in a rectangle 15 pixels wide (the size of the default NSScroller).

+1  A: 

In your NSScroller subclass, you have to override scrollerWidth:

+(CGFloat)scrollerWidth
{
    return 30.0;
}

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

Jason Coco
I have tried overriding this method, but the problem is that NSScrollview always calls this class method on NSScroller, instead of my custom subclass. There is a private _verticalScrollerClass variable in NSScrollView which can be changed, but overriding private variables is not supported.
e.James
Hmm... this may be happening because it's getting archived by IB at that (original) size. Have you tried setting it programmatically to see if that helps?
Jason Coco
I have. Everything I have read suggests that overriding that private ivar is the only way to go. I ended up just creating my own view to duplicate scrollbar functionality and hid the default scrollbar altogether.
e.James
+1  A: 

You can use categories to override the scroller 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
DanieL