Re: Programmatic autolayout problem
Re: Programmatic autolayout problem
- Subject: Re: Programmatic autolayout problem
- From: Kyle Sluder <email@hidden>
- Date: Mon, 25 Feb 2013 21:55:45 -0800
On Feb 25, 2013, at 8:33 PM, Rick Mann <email@hidden> wrote:
> This is the first time I'm trying to programmatically use autolayout, and I can't seem to do a simple thing.
>
> I'm creating a bunch of views using an NSViewController subclass for each, and adding them to a window (the window created from the default Cocoa App template).
Specifically, you are adding them to the window's content view, correct? I'll assume so.
>
> I programmatically build a view layout string that looks like this:
>
> |[view0][view1]
This leaves the right border undefined. Without sufficient internal constraints on your views (either explicit constraints or ones created by virtue of content hugging), this means their widths will also be undefined.
>
> as well as individual constraints that look like:
>
> V:|[view]
This leaves the bottom border undefined. And as above, barring sufficient internal constraints the heights of these views will be undefined.
>
> That is, I just want my views to stack left-to-right (ideally, right-to-left on RTL systems), with no space between them. Prior to this, I resize the window to be just big enough for all the views.
If your views constraints fully specify their widths and heights, this step is unnecessary.
Do you intend for your views to completely define their own sizes, or do you want them to react to the size of the window? Your post seems to imply the former.
>
> Inside each view are a couple of NSTextFields, laid out in IB. What I want to do in IB is something like:
>
> |-5-[text1]-5-[text2]-5-|
>
> And have the parent view compress all the way down to that, such that the text is never truncated.
This leaves the height of the view unspecified.
> But what I'm ending up with are these frames:
>
> containing view: {{0, 20}, {93, 0}}
> text1: {{2, -17}, {34, 17}}
> text2: {{35, -17}, {56, 17}}
>
> Note that the containing view height is 0, and the y-coordinate for the labels is -17; so, nothing's visible.
Because the height of your view is unspecified, auto layout picked one of the infinite values of height that satisfied the system: 0.
>
> Prior to auto layout, it looks like:
>
> containing view: {{0, 0}, {95, 20}}
> text1: {{2, 3}, {34, 17}}
> text2: {{35, 3}, {58, 17}}
>
> Which is how it's laid out in IB.
>
> Can anyone please tell me how to set up the constraints in IB so that the view is as small as it can be, without truncating text or going to 0?
I'd start with a diagram of how you want your window to look. Then draw struts between all the views. Remove unnecessary ones. That'll give you an approximation for the set of constraints you need to install.
> A separate question: Is it possible to get the window to expand to just hug the stacked views?
When your constraints are set up correctly, this will happen automatically, as long as the constraints producing that size have priorities greater than 500 (NSLayoutConstraintPriorityThatCanResizeWindow or some such).
--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