自动布局 - 使视图高度相对于超级视图的一半高度 [英] autolayout - make height of view relative to half superview height

查看:20
本文介绍了自动布局 - 使视图高度相对于超级视图的一半高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近一直在使用自动布局,但我被困在一个看似非常微不足道的问题示例上.我有一个视图,我想坐在屏幕的顶部,并占据屏幕高度的一半.在自动布局之前很简单 - 只需将其固定到位并告诉它在超级视图调整大小时垂直扩展.

have been getting into autolayouts recently and I'm stuck on what seems like a really trivial problem example. I have a view that I want to sit at the top of the screen, and take up half of the screen-height. Simple before autolayout - just tack it in place and tell it to expand vertically when the superview resizes.

现在,我这辈子都不知道该怎么做.这是我尝试设置时得到的结果:

Now, I can't for the life of me see how to do it. Here's what I get when I try to set this up:

底部空间约束设置为等于 284",当我更改为 iPhone4 布局时,这对我来说绝对无用,因为它在屏幕底部保持 284 点空间,并将视图缩小到不再是屏幕大小的一半.并且没有办法将该约束设置为等于任何其他视图高度的一部分..

The bottom space constraint is set to "equals 284", which is absolute and absolutely useless to me when I change to iPhone4 layout, as it keeps 284 points space at the bottom of the screen, and shrinks the view to no longer be half the size of the screen. And there's no way of setting that constraint to equal some fraction of any other view's height..

挣扎了一段时间后,我能想到的唯一方法是在该视图下方引入另一个视图,固定它们的高度,让它们彼此上下放置,然后设置第二个(底部)视图是隐形的..这看起来有点难看!

After struggling for a while, the only way I can think of doing this would be to introduce another view below this view, pin their heights equally, have them sit above and below each other and then set the second (bottom) view to be invisible.. which seems a bit ugly!

我错过了什么明显的东西吗?..

Am I missing something obvious?..

推荐答案

现在这在 IB 中是可能的 [至少] Xcode 5.1.1.虽然我花了一些时间才发现它实际上非常简单:

This is now possible in IB as of [at least] Xcode 5.1.1. Although it took me sometime to figure out it is actually super simple:

首先创建一个基本的顶部对齐约束(您还需要设置底部、左侧和右侧约束,就像正常一样).然后选择约束并导航到 Attribute 检查器:

First create a basic top alignment constraint (you will also need to setup bottom, left, and right constraints, like normal) . Then select the constraint and navigate to the Attribute inspector:

然后您可以调整乘数.如果你想要 50% 的超级视图将其保留在 1,因为它是按照超级视图的中心对齐的.这也是创建其他百分比的视图的好方法(例如 25% 的超级视图)

Then you can adjust the multiplier. If you want it 50% of the super view leave it at 1, since it is aligned per the super's center. This is also a great way to create views that are other percentages too (like 25% of super view)

这篇关于自动布局 - 使视图高度相对于超级视图的一半高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆