Does anyone understand scroll views? I struggle every time I use them, and it
seems that I have to go through a process of trial and error to get them
working properly.
If I drag a scroll view into another view in Interface Builder, I get a
“Bordered Scroll View” that contains a “Clip View” that contains a “View” -
which seems fine. The clip view will clip the content (“document”) view. So I
can see what’s happening, I create a subclass for the document view, which
looks like this (for debugging purposes):
class ContentView: NSView
{
// override var isFlipped: Bool { return true }
override func draw(_ dirtyRect: NSRect)
{
super.draw(dirtyRect)
NSColor.red.set()
NSBezierPath.stroke(self.bounds)
}
}
isFlipped is commented out at this stage, but I’ve added it in case I need it
later.
The document view’s class is set to ContentView in the Identity inspector in
Interface Builder.
By default, Interface Builder gives the content/document view an autoresizing
mask where all the red lines are turned on in the Size inspector - meaning that
the width and height are flexible, and also (I think) that the margins are not
flexible. What happens when I build and run without changing this is that the
content view resizes to match the parent view - so the scroll view is
completely redundant (nothing ever scrolls). That’s fine for horizontal
scrolling, which I don’t want, but I do want to have vertical scrolling, so I
turn off the adjustable height arrow.
What I now have is a view that scrolls vertically, but is pinned to the bottom
of its parent view. Uncommenting isFlipped in ContentView fixes this problem,
and I think everything is now working correctly. I’ve looked for a flipped
setting in Interface Builder, but I haven’t found it, so I assume it’s missing
for some reason that I don’t understand.
But there are other alternatives that seem confusing. Before turning on
isFlipped in code, I tried turning off the bottom margin red line in Interface
Builder. This gives me a view that is still pinned to the bottom of its parent
view - but when the window is shrunk smaller than the height of the content
view, the bottom rather than the top of the content view is scrolled out of
view. If I turn isFlipped on at this point, I get a content view that is pinned
to the top of its parent view (good), but when the window is shrunk smaller the
top of the content view is scrolled out of view (not good). If I go back and
turn the bottom red line back on and turn the top red line off (in IB), it
works correctly.
Summarising: it seems that to get a vertically scrolling view that works as
expected, the content view must be set to be flipped (no way of doing this in
IB) and the flexible-height arrow must be turned off. The top-margin line can
also be turned off, but it actually makes no difference if it is on or off.
Does anyone have a way to make sense of this or is it just inherently confusing?
Jeremy
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]