7

Scroller

Hi! I am using an NSScrollView which has a view-based NSTableView. Whenever the table adds a table cell the scrollers show up which is fine until you see that the scrollers don't have a transparent background. For some reason it's background is white as you can see on the image.

I used IB to set up the NSScrollView and NSTableView and I cannot find any option where I can disable the scroller's background. Any ideas what may have cause this?

Update: Settings for my NSScrollView in Xcode

Xcode

Community
  • 1
  • 1
iMaddin
  • 982
  • 1
  • 14
  • 23

4 Answers4

19

Override NSScroller and do the following. This will give you a transparent background.

- (void) drawRect: (NSRect) dirtyRect
{
    [self drawKnob];
}
David
  • 14,205
  • 20
  • 97
  • 144
  • May you expand on this? I have seen a few answers like this. Surely copying this into my program won't work without it actually being called from somewhere? – maxisme Nov 09 '15 at 14:02
  • @Maximilian Just set it as the scrollbar class in interface builder. – Supertecnoboff Oct 09 '17 at 10:16
  • Great thanks! It's no need to override the `+ (BOOL)isCompatibleWithOverlayScrollers` method. – Itachi Dec 07 '17 at 08:56
  • The `drawBackground` property of the NSScrollView must be set to false, otherwise this doesn't work. – Ely Apr 25 '18 at 13:11
3

My only suggestion is to make sure that your NSScrollers are of the default style, or set to a class that overrides + (BOOL)isCompatibleWithOverlayScrollers if you are using a custom scroller.

In IB, check that "Default Style" is selected under "Scroller Knobs", second from the top.

enter image description here


OS X Lion introduced the new NSScrollers (overlay scrollers) in contrast to the versions present in Snow Leopard and before SL (legacy scrollers). The overlay scrollers are the type that fade in/out, and draw on top of the content without the glaring white background.

In some scenarios Lion decides to use the legacy scroller instead of the new overlay scrollers (because, e.g., if a pointer device w/o scroll wheel is plugged in, it can't exactly scroll a hidden overlay scroller)

The OS X 10.7 documentation explains it well. To summarize, check if a) the user disabled overlay scrollers, b) if you have an unsupported device plugged in, or c) you're subclassing NSScroller, adding accessory views, or forgetting to override + (BOOL)isCompatibleWithOverlayScrollers.

If you dislike the legacy scrollers showing up and want to support non-supported devices (e.g. a gray scrollbar drawn on top of content that doesn't fade in/out) you must subclass NSScroller and draw it custom-ly. Applications like Twitter for Mac do this.

Vervious
  • 5,559
  • 3
  • 38
  • 57
  • I am not using custom scrollers. Using the default style didn't change anything unfortunately. I updated my post with an image of my settings for the NSScrollView. I know it wasn't always like that. – iMaddin Jun 29 '12 at 00:10
  • Are you building for Snow Leopard or anything like that? – Vervious Jun 29 '12 at 00:12
  • 2
    It might also be nice to make sure that a) a user didn't explicitly set not to use overlay scrollers, b) that a mouse isn't connected, and c) the scroll view has no accessory views – Vervious Jun 29 '12 at 00:17
  • 1
    Thank you! My USB mouse was connected! I unplugged it and it vanished right away! How do I prevent this from looking so ugly though when a mouse is connected? – iMaddin Jun 29 '12 at 01:04
  • Unfortunately, you can't, short of writing your own scroller. Lion's behavior when it finds a device that doesn't support scrolling (e.g. a pointer device, even if it has a scroll wheel) is to display a always-present legacy bar. – Vervious Jun 29 '12 at 01:07
  • Though it might be a good idea to write a custom NSScroller - Twitter for Mac, Sparrow, etc. all do it, and it looks better no matter what device is plugged in. – Vervious Jun 29 '12 at 01:07
  • Okay great thanks for your help! I might try that and hopefully I can get it to work. – iMaddin Jun 29 '12 at 01:13
  • 1
    One of my scrollviews in my app are always legacy (others aren't) and none of those conditions apply: scroller and scrollview not subclassed at all, no accessories, nothing but a trackpad. I'm going to try using https://github.com/rheinfabrik/RFOverlayScrollView – Pierre Houston Jan 26 '13 at 02:01
2

For me, in my view controller I set the scroller style like:

[_scrollView setScrollerStyle:NSScrollerStyleOverlay]; 

This made the scrollers have the transparent background.

Chris Livdahl
  • 4,662
  • 2
  • 38
  • 33
-4

If you want to achieve transparency in NSScrollView or simply want a clear background for scroll view...just make use of setDrawsBackground property of NSScrollView,i.e.:

[scrollView setDrawsBackground:NO];

Hope this helps someone. Thanks :)

Eshwar Chaitanya
  • 942
  • 2
  • 13
  • 34
  • 1
    The question is about the background of the NSScroller, not of the NSScrollerView. Your answer doesn't change the background of the NSScroller. – Ely Apr 18 '17 at 08:14