3

I'm using an NSScrollView in my Cocoa application, and I want to customize the length of the horizontal scroller. The content of the scroll view is broken up into two main areas: a fixed-width area on the left and a dynamic-width area on the right. The left area only scrolls vertically -- it stays fixed on the left side of the view as the user scrolls left and right. Here's a screenshot of the view being scrolled:

http://jarodlong.com/dropbox/nsscroller_issue.png

I don't want the horizontal scroller to overlap the fixed area on the left. It should be confined to the area on the right.

Is there any way to do this without subclassing NSScrollView? I'd really prefer to not have to subclass, but if it's necessary, what should I look into when doing so?

I've tried just setting the frame of the horizontal scroller, but I think NSScrollView is constantly sizing the scroller to fit the entire area.

jlong64
  • 928
  • 10
  • 13

2 Answers2

4

I think in this situation I'd use two separate scrollviews, one inside the other.

Ok, I checked with a friend and it turns out that on Lion, you still have NSScroller instances in an NSScrollview, although they're drawn on a GL surface. Not tested, but something like this should do the trick:

@implementation MyScrollView : NSScrollView

- (void) tile
{
[super tile];
id scroller = [self horizontalScroller];
NSRect scrollerRect = [scroller frame];
   // adjust scrollerRect as you want to here
[scroller setFrame:scrollerRect];
}
NSResponder
  • 16,861
  • 7
  • 32
  • 46
  • That probably would have been the best way to go if I had thought to do that from the beginning, but all of the content is a single view and it would be quite a bit of work to refactor it into multiple views at this point. Everything works perfectly except the scrollbar size, so if there's a way to just resize the scrollbar that seems like it would be the easiest solution right now. – jlong64 Aug 16 '11 at 16:24
  • Well, back on Snow Leopard you could just subclass NSScrollview and override -tile, but I'm not sure the new auto-hiding scrollers are still NSScroller instances. – NSResponder Aug 18 '11 at 06:40
  • Actually that worked perfectly! I somehow missed the tile method (maybe because the name isn't very indicative of its purpose), but the new Lion scrollers appear to still be NSScrollers. – jlong64 Aug 18 '11 at 23:44
  • The code edited into your answer works great. Thanks a bunch! – jlong64 Aug 19 '11 at 00:37
1

I'd add two NSScrollViews to your window. The one on the left that only supports vertical scrolling and the one on the right that scrolls horizontally.

lbrndnr
  • 3,361
  • 2
  • 24
  • 34