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

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

问题描述

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

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:

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

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天全站免登陆