ios 在纵向和横向使用自动布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18213376/
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
Working with AutoLayout in Portrait and Landscape
提问by Gaurav Wadhwani
Auto Layout, as good as it is, driving me crazy with constraints. I have the portrait mode designed in IB but I can't get the desired landscape orientation to work.
自动布局,尽管它很好,但约束让我发疯。我有在 IB 中设计的纵向模式,但我无法获得所需的横向工作。
Notice that when the orientation changes to landscape, the spacing between the UIImageView blocks decreases and the text alignment of SAMPLE TEXT
changes to right aligned.
请注意,当方向更改为横向时,UIImageView 块之间的间距减小并且文本对齐SAMPLE TEXT
更改为右对齐。
Currently, I have added a few constraints for it to work. However, I need to keep the space between UIImageView blocks fixed, as a result of which it looks cramped up in portrait and fine in landscape. I need it to be spread out evenly in portrait and compress in landscape (like in image above). Similarly, currently my constraints bring up the text but it doesnot keep it at the center of screen and right aligned.
目前,我为它的工作添加了一些约束。但是,我需要保持 UIImageView 块之间的空间固定,因此它在纵向上看起来很局促,在横向上看起来很好。我需要它在纵向上均匀分布并在横向上压缩(如上图所示)。同样,目前我的约束会显示文本,但不会将其保持在屏幕中心并右对齐。
Is this possible at all with Auto Layout? If so, how? Help greatly appreciated.
使用自动布局可以做到吗?如果是这样,如何?非常感谢帮助。
采纳答案by rdelmar
There are several ways to approach this. One way is to enclose your 3 image views in a UIView. Give the top and bottom image views constraints to the top and bottom respectively, and give the middle one a centerY constraint. Give this enclosing view a fixed height and width, and a constraint to the top of the controller's view. Make an IBOutlet to the height constraint for this enclosing view, and change it on rotation. In the example below, I gave it a height of 350 in portrait:
有几种方法可以解决这个问题。一种方法是将 3 个图像视图包含在 UIView 中。分别给顶部和底部的图像视图约束顶部和底部,并给中间的一个 centerY 约束。给这个封闭视图一个固定的高度和宽度,以及控制器视图顶部的约束。为这个封闭视图创建一个高度约束的 IBOutlet,并在旋转时更改它。在下面的示例中,我将其纵向设置为 350:
-(void)updateViewConstraints {
[super updateViewConstraints];
if (self.view.bounds.size.height < self.view.bounds.size.width) {
self.heightCon.constant = self.view.bounds.size.height;
}else{
self.heightCon.constant = 350;
}
}
As for the label, the easiest way is to remove the constraints you have (bottom and centerX), and add trailing and centerY on rotation.
至于标签,最简单的方法是删除您拥有的约束(bottom 和 centerX),并在旋转时添加 trailing 和 centerY。
回答by mluisbrown
Yes, it can be done with Auto Layout. If you want the spacing between views to increase and/or decrease depending on orientation, you can use the Less Than or Equal or Greater Than or Equal relation (instead of Equal) for the constraint, which allows a distance to grow or shrink:
是的,它可以通过自动布局来完成。如果您希望视图之间的间距根据方向增加和/或减少,您可以对约束使用小于或等于或大于或等于关系(而不是等于),这允许距离增加或缩小:
Play around with that and you should be able to get what you want.
玩弄它,你应该能够得到你想要的。
回答by CaptJak
Yes, it is definitely possible to do this with Auto Layout. Through a series of steps, that I think might be too long to post as an answer, you can retain the spacing between your ImageViews and keep the alignment of the text the same.
是的,使用自动布局绝对可以做到这一点。通过一系列步骤,我认为可能太长而无法作为答案发布,您可以保留 ImageViews 之间的间距并保持文本的对齐方式相同。
Essentially, you will have to pin a corner of each ImageView and remove some constraints so that it doesn't automatically compress the spacing when you change the orientation.
本质上,您必须固定每个 ImageView 的一个角并移除一些约束,以便在您更改方向时不会自动压缩间距。
Full explanation on how to do this (pretty much exactly what you are asking for) is explained in this tuorial. You can find it about halfway through the page.
本教程中解释了有关如何执行此操作的完整说明(几乎完全符合您的要求)。您可以在页面的一半左右找到它。
Hope this is what you were looking for.
希望这就是你要找的。