Sam
Sam

Reputation: 2865

How can one draw a background in the elastic portion of an NSScrollView?

I have a custom NSScrollView with elasticity turned on in both orientations. Currently I just get a black (ugly) background when I scroll/bounce into this elastic section. How do I control what gets drawn into this section?

For a start, I would just want to be able to set the background colour of this section to a specific colour.

Things I have tried that do NOT seem to work:

  1. Using the background property of the NSSCrollView - it doesn't seem to do anything which is also strange?
  2. Setting the custom NSScrollView.wantsLayer = true and then choosing an appropriate CGColor => also no effect
  3. Adding an independent subView with a background color to NSScrollView that fills it vertically and horizontally (even tried overfilling by using negative leading spaces) => no effect
  4. Trying to use constraints to make the contentView bigger than the NSScrollView => no discernible effect
  5. Making the documentView bigger => it just made the documentView extend for said amount (this was silly, but one does get desperate!)
  6. Setting the background on the NSClipView as suggested in a comment below => no effect

Upvotes: 2

Views: 488

Answers (3)

rdb
rdb

Reputation: 1582

This answer applies if you have a custom NSView assigned as your documentView.

As of the macOS 14 SDK, assuming you haven't overridden clipsToBounds (which now defaults to NO), drawRect will be called with a dirty rectangle that may exceed the bounds of the view, which allows you to draw the overhang that appears during elastic scrolling.

In prior versions of the SDK, it is necessary to instead define a drawBackgroundOverhangInRect method in your NSView subclass to do this drawing instead. In this case, clipsToBounds must be kept at the default value of YES, or this method will not be called (in my experience).

Upvotes: 0

Sam
Sam

Reputation: 2865

Well if at first you don't succeed (and nobody has answered your question on SO yet)...

What worked is to add a subview to the NSClipView (not the NSScrollView) constraining it to be adjacent to the documentView where the elasticity is arising. That's all...

Upvotes: 3

Jef
Jef

Reputation: 4728

Add a subview to the scrollView which is not the documentView, and this view will not scroll or magnify. Constrain that view to the edges so that it is the full size of the scrollView

Upvotes: 2

Related Questions