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 16:41:41 -0700
Hello Charles,
It sounds like you have the idea that constraints establish a minimum and maximum size on a window (or view), and the window is free to take on any size within that range. When you resize a window, it evaluates the constraints at the proposed size. If would make the constraints invalid then it limits the size.
This isn't quite right. In fact, a window has the size it has because it is constrained to be exactly that size. If there is "wiggle room" we would say the constraints are ambiguous because they do not fully specify the size. That's considered a programming error.
As the user resizes a window by dragging it, AppKit modifies those constraints. These constraints have the priority NSLayoutPriorityWindowSizeStayPut. If there are other constraints with higher priority, they take precedence, and will limit the window size. There is no explicit "max size" calculation: the max size falls out of the constraints.
So to answer your question, in most cases it is a bad idea to try to figure out the "maximum size" of a view. It is also a bad idea to call setFrame:, because that will be immediately undone the next time -layout runs. Instead, you should create width and height constraints for the view, set them, call -layoutIfNeeded, and then inspect the resulting size. If you want to allow certain other constraints to limit the size, then set a priority for your constraint that is lower than that of those other constraints.
By the way, fittingSize does not correspond to the view's minimum size, for the same reason.
Hope that helps,
-Peter
On Mar 19, 2012, at 9:20 PM, Charles Srstka <email@hidden> 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.
>
> What I am having trouble figuring out is how to resize such a view in code in such a way that it will respect the constraints. If I try just setting a frame using -setFrame: with a rect which has an illegal size, NSView is all too happy to do that, and then I get an exception and a flood of console warnings about how I just broke my constraints. Now, getting the view’s minimum size by calling -fittingSize and adjusting my frame accordingly is easy enough, but some views can have a maximum size imposed by constraints as well, and I’m having trouble finding a way to find out what it is in code.
>
> I know that this has to be possible, since Interface Builder and NSWindow are both able to gracefully handle cases where one tries to set a frame size outside the boundaries of what the constraints allow, but I haven’t been able to find any -maximumSize or -adjustedSizeForSize: type methods, or any way to figure this out short of wrapping -setFrame: in an exception handler. I’m sure it’s gotta be something fairly obvious, and I’ll probably be embarrassed by the answer, but at the moment I’m a bit stuck.
>
> Anyone know how to do this?
>
> Thanks,
> Charles
_______________________________________________
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