简体   繁体   English

iOS自动布局约束

[英]iOS auto-layout constraints

I'm unsure of how I should set up some constraints in my auto-layout storyboard in iOS. 我不确定如何在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. 我旁边有一个文本框和一个按钮,彼此之间受限制,并且它们都被限制在其超级视图的两侧,直到20岁。它非常适用于iPhone / iPod。 Moving up to iPad, however, the textbox becomes too wide. 但是,转到iPad时,文本框变得太宽。 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. 这将为您的UI元素添加最大宽度。

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

The constraint 约束

You can set a constraint for "width less than or equal to X" (whatever your value X is). 您可以为“宽度小于或等于X”设置约束(无论X值是多少)。

Then pair that with your "trailing edge space to the edge of the superview is equal 20". 然后,将其与“到超级视图边缘的尾部边缘空间等于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. 但是,您可以做的是给“边缘空间”约束的优先级比宽度约束的优先级小1。

So if the width constraint has a priority of 750 (which is default) then give then space constraint a priority of 749. 因此,如果宽度约束的优先级为750(默认值),则为空间约束的优先级为749。

This has the effect of telling AutoLayout. 这具有告诉AutoLayout的效果。 "If there is a conflict, then break gracefully by 'removing' the space constraint and keeping the width constraint". “如果存在冲突,则可以通过“删除”空间约束并保持宽度约束来优雅地打破”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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