简体   繁体   English

iOS故事板限制/纵向和横向视图的特征

[英]iOS Storyboard Constraints/Traits for portrait and landscape view

my iOS project consists at the moment of one single UIView or UIViewController. 我的iOS项目包含一个单独的UIView或UIViewController。 The controller class is called MainViewController.swift. 控制器类称为MainViewController.swift。 The view of the view controller has a subview which I've placed at the bottom: 视图控制器的视图有一个我放在底部的子视图: 在此输入图像描述

I've set the auto layout option to keep the distance of the subview for every device size the same: 我设置了自动布局选项,以保持每个设备大小的子视图的距离相同: 在此输入图像描述

Now I want to keep the sub view always on the short side of the parent view, which means that in landscape mode the sub view show be on the right side of the parent view: 现在我想保持子视图始终位于父视图的短边,这意味着在横向模式下,子视图显示在父视图的右侧: 在此输入图像描述

I've checked some tutorials and other posts in the meanwhile: 我同时检查了一些教程和其他帖子:

At this point it is not clear to my how to realize such a behavior best? 在这一点上,我不清楚如何最好地实现这种行为? Possibilites I've found to solve the issue: Possibilites我发现解决了这个问题:

  • Vary for traits...but how? 对特质不一样......但是如何?
  • Constraints? 约束?
  • Programmatically - I think this would be the fastest solution, but I really want to use storyboards and learn the correct usage. 以编程方式 - 我认为这将是最快的解决方案,但我真的想使用故事板并学习正确的用法。

Here you can play with size classes and traits. 在这里,您可以玩大小类和特征。 if you want give constraint in portrait mode you have to select compact width and regular height and for landscape compact width compact height. 如果你想在纵向模式下给予约束,你必须选择紧凑的宽度和常规高度以及景观紧凑宽度的紧凑高度。

Please go through following steps to constraint portrait and landscape separately. 请按照以下步骤分别限制纵向和横向。

1) Please select mode either landscape and portrait from traits. 1)请从特征中选择横向和纵向模式。

2) Now, you can constraint each mode one by one. 2)现在,您可以逐个约束每个模式。 suppose i am giving constraint to red view in portrait mode and i will enable particular constraints for for portrait size class so it will be disable for landscape mode. 假设我在纵向模式下为红色视图提供约束,并且我将为纵向尺寸类启用特定约束,因此它将对横向模式禁用。

在此输入图像描述 3)once we are finished with portrait mode i have switched to compact height compact regular size class which is landscape mode. 3)一旦我们完成了纵向模式,我已经切换到紧凑高度紧凑的常规尺寸类,这是横向模式。

在此输入图像描述

4) Now, we can constraint the landscape mode as shown in above screenshot. 4)现在,我们可以约束横向模式,如上面的屏幕截图所示。

As we can see constraints which is light in color are disable for current size class. 我们可以看到,对于当前大小类,禁用颜色浅的约束。

So this is how we can constraints landscape and portrait mode separately. 这就是我们如何分别约束横向和纵向模式。

Because there is no size class or trait difference between an iPad in landscape, and an iPad in portrait, you will ultimately have to handle at least some cases programatically. 因为横向iPad和纵向iPad之间没有大小等级或特征差异,所以最终必须以编程方式处理至少一些情况。 That being the case, you might as well just handle all cases programatically with a method that does something like: 既然如此,你也可以用一种方法来编程处理所有情况,例如:

  1. Get current screen size 获取当前屏幕尺寸
  2. If width > height, add constraints to center the subview vertical and pin it to the right edge. 如果width> height,则添加约束以使子视图垂直居中并将其固定到右边缘。 Otherwise, add constraints to center the subview horizontally and pin it to the bottom edge. 否则,添加约束以水平居中子视图并将其固定到底边。

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

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