简体   繁体   English

Xcode-iPad上的约束-横向vs纵向

[英]Xcode - Constraints on iPad - Landscape vs Portrait

I can't seem to figure out constraints and auto layout.How can I have the spacing between two objects be different on the iPad when in landscape and portrait. 我似乎无法弄清楚约束和自动布局。在横向和纵向模式下,如何在iPad上使两个对象之间的间距不同。

I placed to UIViews side by side. 我并排放置到UIViews中。 When in portrait I would like the spacing between them to be 40 but when in landscape it can be larger because there is more width. 纵向时,我希望它们之间的间距为40,但横向时,则可以更大,因为宽度更大。

This is what it looks like in portrait mode. 这就是纵向模式下的样子。 Which I am happy with. 我很满意。 肖像

And here it is in landscape. 这里是风景。 As you can see it doesn't seem to fill the screen as well and there ends up being a lot of empty space. 如您所见,它似乎也无法填满整个屏幕,最终会留下很多空白。 景观

Any suggestions? 有什么建议么? Thank you! 谢谢!

you can click the small + icon next to the constraint value and select different values for another size class. 您可以单击约束值旁边的小+图标,然后为另一个尺寸类别选择不同的值。

or you can change the constraint value in code depending on your orientation. 或者您可以根据自己的方向在代码中更改约束值。

or you can add extra constraints with lower priority and then increase the priority once you are in landscape mode. 或者您可以添加其他具有较低优先级的约束,然后在处于横向模式时增加优先级。

Instead of giving the spacing constraint a constant value like 40, make it a proportional width constraint that is some small percentage of the width of one of the views it is installed between, or a small percentage of the width of the overall superview. 与其给间距约束一个常数值(例如40),不如给它一个比例宽度约束,它是它所安装的视图之一的宽度的很小百分比,或者是整个超级视图的宽度的很小百分比。 That way, as the width of the screen gets larger (as in landscape orientation), the spacing proportionately gets wider as well. 这样,随着屏幕的宽度变大(如横向),间距也成比例地变宽。

In addition to @Daniel Hall's answer, I would also like to point out that you could also pin the leading and trailing edges of the two UIView you have to their parent view and each other. 除了@Daniel Hall的答案之外,我还要指出,您也可以将两个UIView的前缘和后缘固定到其父视图和彼此之间。

For example: 例如:

First UIView : Leading Edge 10 points from parent view, Trailing Edge 40 points from second UIView 第一个UIView :父视图的前10个点,第二个UIView边缘40个点

Second UIView : Leading Edge 40 points from first UIView , Trailing Edge 10 points from parent view 第二个UIView :从第一个UIView到前沿40点,从父视图到尾随10点

It should resize itself based on these auto layout constraints once you rotate your device. 旋转设备后,它应根据这些自动布局约束调整自身大小。 Of course with this method, you run the risk of having your two UIView stretched beyond accurate proportion, so use it wisely. 当然,使用此方法,您可能会冒着两个UIView超出准确比例的风险,因此请明智地使用它。

I ended up having to pin it to the to the bottom and their respective sides. 我最终不得不将其固定在底部和它们各自的侧面。

View 1: 查看1:

  • Leading: 30 领先:30

  • Bottom: 25 底部:25

View 2: 查看2:

  • Trailing: 30 尾随:30
  • Bottom: 25 底部:25

I used a >= constraint for the space between them (>= 5). 我对它们之间的空间使用了> =约束(> = 5)。 If I increased the constant on any of these constraints the views would overlap. 如果我增加这些约束中的任何一个的常数,则视图将重叠。 It's not the best situation because I still have a lot of empty space in landscape mode but it's the best I could come up with. 这不是最好的情况,因为在风景模式下我仍然有很多空白,但这是我能想到的最好的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM