Re: NSScrollView and autolayout
Re: NSScrollView and autolayout
- Subject: Re: NSScrollView and autolayout
- From: Chuck Soper <email@hidden>
- Date: Tue, 26 Feb 2013 17:21:42 -0800
- Thread-topic: NSScrollView and autolayout
Excellent, informative response. Thanks.
Do you know if your documentView returns YES for hasAmbiguousLayout?
My user interface appears to work fine. The only problem is that
[scrollView.documentView hasAmbiguousLayout] returns YES.
Given that my UI appears to work as expected, can I just ignore the fact
that [scrollView.documentView hasAmbiguousLayout] returns YES?
more comments below...
On 2/26/13 2:52 PM, "Kyle Sluder" <email@hidden> wrote:
>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.
Ah, I understand now. I've seen that exception. Good point. I'm already
overriding -updateConstraints in my custom documentView. I maintain an
array of constraints similar to the AutoLayout SplitView sample code (WWDC
2011). It seems like I might be able to do the following in my
documentView's updateConstraints override:
1. Remove all existing constraints (and empty my constraints array).
2. Set the frame for my documentView.
3. Create and add new constraints (loading my constraints array).
This might avoid the unsatisfiable constraints exception.
If this approach works, then I'd also have to be careful about subviews
within my documentView. They override intrinsicContentSize. I call
invalidateIntrinsicContentSize to update their size. In this case, I might
not be able to avoid the unsatisfiable constraints exception unless I
completely rebuilt my constraints every time a subview's size changes -
not very efficient.
>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.
I'm overriding updateConstraints in my document view as well. I just added
this call: [self setTranslatesAutoresizingMaskIntoConstraints:NO] before
calling super, as you recommended. I believe that the constraints for my
documentView and its subviews are correct. Everything seems to work except
that the documentView returns YES for hasAmbiguousLayout.
Can you say anything more about how to set up constraints that keep your
document view correctly sized and positioned relative to the enclosing
clip view? I'm not following that.
>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.
I totally agree. Hey, maybe for 10.8.3! As far as I can tell the issue or
a work-around isn't documented.
>>
>> With NSSplitView, I think it's fine to add subviews that use auto
>>layout.
>
>Yes, on 10.8. 10.7 is trickier.
>
>--Kyle Sluder
Thanks!
Chuck
_______________________________________________
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