xcode 故事板自动布局:“尾随空间到容器”与“底部空间到底部布局”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19167911/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:00:27  来源:igfitidea点击:

Storyboard Auto Layout: "trailing space to container" vs "bottom space to bottom layout"

iphoneiosxcodeinterface-builderautolayout

提问by bbrame

Could someone help me understand the difference between "trailing space to container" and "bottom space to bottom layout" in interface builder auto layout?

有人可以帮助我理解界面构建器自动布局中“尾随空间到容器”和“底部空间到底部布局”之间的区别吗?

Interface Builder Auto Layout Constraints

界面生成器自动布局约束

I'd like to put a layout constraint in place that ties the bottom of a view to the bottom of it's superview. Both of the above sound like they should accomplish this.

我想放置一个布局约束,将视图的底部与其超级视图的底部联系起来。以上两个听起来都应该实现这一点。

Any clarification would be greatly appreciated.

任何澄清将不胜感激。

回答by rickster

Leading/trailing is horizontal spacing. (It's not labeled "left" and "right" because it swaps for right-to-left languages.) Top/bottom is exactly what it says.

前导/尾随是水平间距。(它没有被标记为“左”和“右”,因为它交换了从右到左的语言。)顶部/底部正是它所说的。

"To Container" and "to Layout Guide" are defined differently, but work similarly in practice. "To Container" constraints control the space between a UI element and the edge of its superview (or other containing UI element). "To Layout Guide" constraints are a special case for vertical spacing -- since views extend underneath transparent navigation/status/tab bars in iOS 7, you generally want to control where a UI element appears relative to those instead of to the edge of the view.

“到容器”和“到布局指南”的定义不同,但在实践中的工作方式相似。“到容器”约束控制 UI 元素与其父视图(或其他包含 UI 元素)边缘之间的空间。“布局指南”约束是垂直间距的特殊情况——因为视图在 iOS 7 中的透明导航/状态/标签栏下方扩展,您通常希望控制 UI 元素相对于这些元素出现的位置,而不是相对于它们的边缘看法。

So...

所以...

I'd like to put a layout constraint in place that ties the bottom of a view to the bottom of it's superview.

我想放置一个布局约束,将视图的底部与其超级视图的底部联系起来。

"Bottom Space to Bottom Layout Guide" is what you want for that. That'll set spacing relative to the bottom of the superview if there's no tab bar, and relative to the tab bar if one exists.

“底部空间到底部布局指南”正是您想要的。如果没有标签栏,这将设置相对于超级视图底部的间距,如果存在则相对于标签栏。

回答by Irfanlone

Trailing Space

尾随空间

Space from the right edge of the current view to the next/neighbor view or the superview

从当前视图的右边缘到下一个/相邻视图或超级视图的空间

Leading Space

领先空间

Space from the a neighboring view or superview to the left edge of the current view

从相邻视图或超级视图到当前视图左边缘的空间

Similarly Top and Bottom correspond to top and bottom space.

类似地,Top 和Bottom 对应于顶部和底部空间。

回答by Artem Zaytsev

Don't forget to uncheck "Relative to margin" enter image description here

不要忘记取消选中“相对于保证金” 在此处输入图片说明

回答by Matt M

To add to the already accepted answer, It is also worth noting that if you are replacing the view in a view controller with another, I have found that any constraints to the layout guides are not actually used. I haven't looked into the exact details of this, but I presume it's because the layout guides are only available during the time the View Controller is loaded from the storyboard.

添加到已经接受的答案中,还值得注意的是,如果您将视图控制器中的视图替换为另一个视图,我发现实际上并未使用对布局指南的任何约束。我还没有研究过这个的确切细节,但我认为这是因为布局指南仅在从故事板加载视图控制器期间可用。

For this reason, I recommend using layout to container, but only when you intend to be swapping views of the view controller.

出于这个原因,我建议使用布局到容器,但仅当您打算交换视图控制器的视图时。

I came across this when I wanted to use the storyboard to create empty states for my apps.

当我想使用故事板为我的应用程序创建空状态时,我遇到了这个问题。

Example:

例子:

-(void)viewDidLoad{ UIView* replacementView = //view loaded from storyboard self.view = replacementView //Layout guide margins will not be followed in replacement view }

-(void)viewDidLoad{ UIView* replacementView = //view loaded from storyboard self.view = replacementView //Layout guide margins will not be followed in replacement view }