Re: How to get max size of view according to constraints?
Re: How to get max size of view according to constraints?
- Subject: Re: How to get max size of view according to constraints?
- From: Peter Ammon <email@hidden>
- Date: Tue, 20 Mar 2012 17:00:22 -0700
On Mar 20, 2012, at 10:01 AM, Charles Srstka wrote:
> On Mar 20, 2012, at 8:16 AM, Richard Somers wrote:
>
>> On Mar 19, 2012, at 10:20 PM, Charles Srstka wrote:
>>
>>> As everyone knows, if you have a view with a bunch of subviews and you’ve got NSLayoutConstraints set up for everything, in many cases you might end up with a minimum or maximum size for the view beyond which the constraints are impossible to satisfy, and if you try to resize the view outside these bounds either in IB or in the actual program (if the view is the content view of a resizable window, for example), the resizing will simply stop at those boundaries.
>>
>> I have worked with constraints in another system and one of the things I learned is that a collection of constraints must be "exercised" or driven from one extreme to the other in order to have confidence that they are correct. If you have a min or max size condition for the view beyond which the constraints are impossible to satisfy, but this in not what you want, then you need to change the constraints so that you get what you want under all conditions. The constraint engine is mathematically correct and does not lie. So if there is a problem, it is in how you are specifying and arranging the constraints or in the number of constraints you have.
>
> Right, but the problem is that I want to make a view that can be given arbitrary subviews at runtime, and I don’t necessarily know what its subviews and their constraints will be at compile time. What I want is a way to determine the range of sizes that this view can have, at runtime. I can find the minimum size via -fittingSize, but I can’t figure out how to get the maximum size.
>
> Specifically, what I’m trying to do is to make a constraints-aware NSScrollView. You can put whatever views you want in it, and it resizes its document view as appropriate as you resize the NSScrollView (say, by resizing the window it’s in). The idea is, it should attempt to resize the document view to match the size of the scroll view. If the user tries to resize the view smaller than what the constraints will allow, then the scroll bars appear. That part is working, but if the user tries to make the scroll view too big, then everything blows up when my code attempts to stretch the document view out.
>
> What I’m trying to find is the upper limit on the width and height according to the current set of constraints at runtime, when I don’t necessarily know what the subviews or their constraints are.
Here's how I'd recommend approaching the scroll view example via constraints.
1. Establish a required constraint that says your document view's top equals the scroll view's top.
2. Establish another constraint that says the height of the document view equals the height of the scroll view. Give this constraint a priority lower than required. What its priority should be depends on how tightly you want the document view to fill the scroll view, i.e. which other constraints it should be allowed to break.
By choosing a priority lower than Required, it will not break required constraints amongst the subviews, and so it will not cause exceptions. You should be able to do all this without ever calling setFrame: or fittingSize.
Hope this helps. Feel free to ask more questions. Auto layout is very powerful, but requires a different way of thinking than the old setFrame: approach.
-Peter
_______________________________________________
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