简体   繁体   中英

iOS auto-layout constraints

I'm unsure of how I should set up some constraints in my auto-layout storyboard in iOS.

I have a textbox and a button next to it, constrained to each other, and they're both constrained to the sides of their superview by 20. It works great for iPhones/iPods. Moving up to iPad, however, the textbox becomes too wide. Is there some way to set a "max width" on the text box in order to allow some growth, and allow the constrains to the superview grow after that point?

It feels like I'm approaching it all wrong, do I have the wrong approach?

You can set a width constraint and in the right side of the screen, instead of equal, select less than or equal (or something like this). This will add a maximum width to your UI element.

您可以添加宽度约束,可以将宽度约束设置为具有相等值的常量,例如100,或者可以添加大于100或小于等于equal的equal ...

The constraint

You can set a constraint for "width less than or equal to X" (whatever your value X is).

Then pair that with your "trailing edge space to the edge of the superview is equal 20".

The Conflict

This will not work as it is though as if the superview is too wide it will create a conflict.

Priorities

What you can do though is give the "edge space" constraint a priority of 1 less than the priority of the width constraint.

So if the width constraint has a priority of 750 (which is default) then give then space constraint a priority of 749.

This has the effect of telling AutoLayout. "If there is a conflict, then break gracefully by 'removing' the space constraint and keeping the width constraint".

您可以在文本字段中使用Ctrl + Drag来设置固定宽度。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM