在 Xcode (Interface Builder) 中更新框架和约束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30338053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update Frames & Constraints in Xcode (Interface Builder)
提问by Martin
I have been learning Xcode and iOS and things are going well with coding but I am confused about a few options in interface builder.
我一直在学习 Xcode 和 iOS,编码进展顺利,但我对界面构建器中的一些选项感到困惑。
It seems that when I add a contraint I can choose to "Update Frames" and then either "Items of new contraints" or "All Frames" but it seems that I don't always need to do this... sometimes I do and sometimes I don't - I don't think I truly understand the concept. It seems to be optional. When is the case to choose it or not?
似乎当我添加约束时,我可以选择“更新框架”,然后选择“新约束的项目”或“所有框架”,但似乎我并不总是需要这样做......有时我会这样做有时我不 - 我认为我没有真正理解这个概念。它似乎是可选的。什么时候选择它或不选择它?
Once a constraint is added, then should that be it?
一旦添加了约束,那么应该是这样吗?
There is also a context menu (Resolve autolayout issues) (3rd item on the bottom on interface builder, represented by a triangle between 2 vertical lines) which also allows me to Force Update of constraint or update of frame. Why is this needed ? I notice the options inside are usually disabled so I presume this becomes active when there are issues ?
还有一个上下文菜单(解决自动布局问题)(界面构建器底部的第三项,由两条垂直线之间的三角形表示),它还允许我强制更新约束或更新框架。为什么需要这个?我注意到里面的选项通常被禁用,所以我认为当出现问题时它会被激活?
I have tried searching the internet and I can't find anything specific; the more I read, the more confused I seem to get.
我尝试在互联网上搜索,但找不到任何具体内容;我读得越多,似乎就越糊涂。
Also, a sanity check: a frame is the bounding box of every single control (view) not just the viewcontrol, so every view (control) as its own frame. Is this correct?
此外,完整性检查:框架是每个单个控件(视图)的边界框,而不仅仅是视图控件,因此每个视图(控件)都是它自己的框架。这样对吗?
回答by Erez Haim
I'll try to explain the concept of constraints and storyboard representation.
Constraints- are used to determine the position of UIView elements inside your view. For example, the vertical distance from top of the view to UIView element top. When you add constraint using the storyboard it plays 2 roles:
我将尝试解释约束和故事板表示的概念。
约束- 用于确定视图中 UIView 元素的位置。例如,视图顶部到 UIView 元素顶部的垂直距离。当您使用故事板添加约束时,它扮演两个角色:
- Positioning the element inside a view when your app runs
- Positioning the element in the storyboard representation itself
- 当您的应用程序运行时将元素定位在视图内
- 在故事板表示本身中定位元素
Update Frames- is used when you want to update storyboarddisplay so that it fits the currently applied constraints. When the display of the storyboard does not match the constraints a warning will appear.
更新框架- 当您想要更新故事板显示以使其适合当前应用的约束时使用。当故事板的显示与约束不匹配时,将出现警告。
Add missing constraints / Update constraints- will create/change the constraints applied on UIView element to match what currently presented in the storyboard. Kind of the other way around from update frames.
添加缺少的约束/更新约束- 将创建/更改应用于 UIView 元素的约束以匹配故事板中当前呈现的内容。与更新框架相反。
- This answer is not a replacement for Apple's Auto Layout Guide
- 这个答案不能替代苹果的自动布局指南
回答by Peter Foti
You do not have to click on "update frame" every time you add a constraint, if you do, it will update the frame of the view to whatever it should be based on the constraints. Yes, every view has a frame and it is the rectangle relative to the views superview.
您不必每次添加约束时都单击“更新框架”,如果这样做,它会根据约束将视图的框架更新为应有的任何内容。是的,每个视图都有一个框架,它是相对于视图超级视图的矩形。
More often than not you'll probably click on update constraints (Shift + Command + =), this will allow you to resize views that are already constrained, and have the constraints updated to match.
通常情况下,您可能会单击更新约束(Shift + Command + =),这将允许您调整已受约束的视图的大小,并更新约束以匹配。