ios 自动布局 - 使视图高度相对于超视图高度的一半
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14935825/
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
autolayout - make height of view relative to half superview height
提问by Mete
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:
现在,我一辈子都看不到该怎么做。这是我尝试设置时得到的结果:
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..
底部空间约束设置为“等于 284”,当我更改为 iPhone4 布局时,这对我来说是绝对且绝对没用的,因为它在屏幕底部保留了 284 点空间,并将视图缩小到不再是一半屏幕的大小。并且无法将该约束设置为等于任何其他视图高度的一部分。
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?..
我错过了一些明显的东西吗?..
回答by Fyodor Volchyok
Storyboard solution where you can set exact ratio between any views:
故事板解决方案,您可以在其中设置任何视图之间的精确比例:
Now:
现在:
PROFIT!!!
利润!!!
P.S. Also note that this method works with views on different nesting levels and (obviously) applicable for width
PS 另请注意,此方法适用于不同嵌套级别的视图,并且(显然)适用于宽度
P.P.S. sometimes it might be helpful to "reverse first and second item" of constraint or set reverse multiplier (for example 2 instead of 0.5) (but these methods are not helpful if you don't understand how views relate between each other).
PPS 有时“反转约束的第一项和第二项”或设置反向乘数(例如 2 而不是 0.5)可能会有所帮助(但如果您不了解视图之间的关系,这些方法将无济于事)。
回答by Firo
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:
从 [至少] Xcode 5.1.1 开始,这现在可以在 IB 中实现。虽然我花了一些时间才弄清楚它实际上非常简单:
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 Attributeinspector:
首先创建一个基本的顶部对齐约束(您还需要像平常一样设置底部、左侧和右侧的约束)。然后选择约束并导航到属性检查器:
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)
然后你可以调整乘数。如果你想要它,50% 的超级视图将它留在1
,因为它是按照超级的中心对齐的。这也是创建其他百分比的视图的好方法(例如超级视图的 25%)
回答by Mete
After a bit more time I've come up with the following.
过了一会儿,我想出了以下内容。
I'm noting it as an answer but it's not very satisfying, since it assumes you can't actually do this in Interface Builder, but the correct constraint can be added in code afterwards as:
我注意到它是一个答案,但它并不是很令人满意,因为它假设您实际上无法在 Interface Builder 中执行此操作,但是可以在之后的代码中添加正确的约束,如下所示:
- (void) viewWillAppear:(BOOL)animated
{
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:upperview
attribute:NSLayoutAttributeHeight
relatedBy:0
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:.5
constant:0];
[self.view addConstraint:constraint];
}
Basically, it sets a multiplier of 0.5 against the height of self.view, acting on upperview. I had to set the priority of the bottom vertical space constraint in IB to lower than 1000 to avoid a bunch of runtime messages about breaking constraints as well.
基本上,它针对 self.view 的高度设置了 0.5 的乘数,作用于上视图。我不得不将 IB 中底部垂直空间约束的优先级设置为低于 1000,以避免出现一堆关于打破约束的运行时消息。
So if anyone can show how to do this in Interface Builder, that would better answer my question, otherwise I guess this is as good as it gets (for now)???
因此,如果有人可以展示如何在 Interface Builder 中执行此操作,那将更好地回答我的问题,否则我想这已经(目前)如此出色???
回答by user444333222
Slightly easier and more straight forward than method than Fyodor Volchyok's answer. -Hold down the control button and click on the subview. -Still holding down on the command button, drag cursor to the superview then click on the superview. -Select "Aspect Ratio".
比 Fyodor Volchyok 的答案更简单、更直接。- 按住控制按钮并单击子视图。- 仍然按住命令按钮,将光标拖动到超级视图,然后单击超级视图。- 选择“纵横比”。
-Then click the Size Inspector.
-Then double click on the constraint.
-Make sure that "height" is selected for both items.
-Then change the "Multiplier" to 0.5 for half the screen, or whatever fraction of the superview you desire.
回答by brainray
There i also another possibility in code in case you have 2 views that should both have the same height: just set the height of view2 to the height of view1 (the trick here is not setting the height of view1 explicitely).
如果您有两个视图应该具有相同的高度,我还有另一种可能性:只需将 view2 的高度设置为 view1 的高度(这里的技巧不是明确设置 view1 的高度)。
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:[topLayoutGuide]-0-[_view1]-0-[_view2(==_view1)]-0-[bottomLayoutGuide]"
options:0
metrics:nil
views:viewsDict]];
回答by user444333222
Swift version of Mete's answer:
Mete 答案的 Swift 版本:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
upperView.translatesAutoresizingMaskIntoConstraints = false
var constraint = NSLayoutConstraint(item: upperView,
attribute: NSLayoutAttribute.top,
relatedBy: NSLayoutRelation.equal,
toItem: self.view,
attribute: NSLayoutAttribute.top,
multiplier: 1,
constant: 0)
self.view.addConstraint(constraint)
constraint = NSLayoutConstraint(item: upperView,
attribute: NSLayoutAttribute.height,
relatedBy: NSLayoutRelation.equal,
toItem: self.view,
attribute: NSLayoutAttribute.height,
multiplier: 0.5,
constant: 0)
self.view.addConstraint(constraint)
constraint = NSLayoutConstraint(item: upperView,
attribute: NSLayoutAttribute.leading,
relatedBy: NSLayoutRelation.equal,
toItem: self.view,
attribute: NSLayoutAttribute.leading,
multiplier: 1,
constant: 0)
self.view.addConstraint(constraint)
constraint = NSLayoutConstraint(item: upperView,
attribute: NSLayoutAttribute.trailing,
relatedBy: NSLayoutRelation.equal,
toItem: self.view,
attribute: NSLayoutAttribute.trailing,
multiplier: 1,
constant: 0)
self.view.addConstraint(constraint)
}