Re: NSScrollView and autolayout
Re: NSScrollView and autolayout
- Subject: Re: NSScrollView and autolayout
- From: Kyle Sluder <email@hidden>
- Date: Tue, 26 Feb 2013 14:52:00 -0800
On Tue, Feb 26, 2013, at 02:32 PM, Chuck Soper wrote:
> Does NSScrollView own its documentView, and controls the value that
> documentView returns for setTranslatesAutoresizingMaskIntoConstraints:?
It's actually NSClipView that controls this, but yes.
>
> I've created an NSView subclass that I'm using for a documentView within
> an NSScrollView. I'm calling [self
> setTranslatesAutoresizingMaskIntoConstraints:NO] during initialization.
> Is
> that a problem? The UI works fine, but the layout for my documentView is
> ambiguous. I believe that I'm setting its constraints properly.
The general philosophy behind -translatesAutoresizingMaskIntoConstraints
is that it's the container's job to control that property. That allows
containers which position their subviews to incrementally adopt auto
layout.
> Would it be better to [scrollView.documentView
> addSubview:myContainerView]? Then, add the following constraints to the
> scrollView.documentView:
> @"V:|[myContainerView]|" and @"H:|[myContainerView]|"
> This way, I think that it would be fine to call [self
> setTranslatesAutoresizingMaskIntoConstraints:NO] during initialization
> for
> myContainerView.
> If this is true, then I think that I may need to call setFrameSize: on
> scrollView.documentView when my myContainerView frame size changes, is
> that correct? Or, is there some way to get my documentView to resize
> based
> on its subviews (i.e. myContainerView)?
Sadly, this won't work. The problem here is that the constraints
installed by -translatesAutoresizingMaskIntoConstraints are fixed
constraints, and if myContainerView's constraints solve to a different
value that the frame of the scroll view's document view, you will get an
unsatisfiable constraints exception.
In order to get auto layout to work with scroll views, we actually
override -updateConstraints in our document view, and call [self
setTranslatesAutoresizingMaskIntoConstraints:NO] before calling super.
Then we set up constraints that keep us correctly sized and positioned
relative to our enclosing clip view.
This is a terrible hack that I really hope is fixed in a future version
of OS X with a constraint-aware implementation of NSScrollView that
works like UIScrollView on iOS 6.
>
> With NSSplitView, I think it's fine to add subviews that use auto layout.
Yes, on 10.8. 10.7 is trickier.
--Kyle Sluder
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden