简体   繁体   English

具有大小类的iOS自动布局不会区分3” 5”和“ 4”的风景吗?

[英]iOS autolayout with size classes does not differenciate landscape for 3“5 and 4”?

here's what I've been struggling with for the past hours. 这就是我过去几个小时一直在努力的事情。

I have a view controller that contains a few labels, a picture, and a tableview. 我有一个视图控制器,其中包含一些标签,图片和tableview。 I'm using autolayout/size classes in order to use only one storyboard for the various devices supported (meaning: all size of iphones and iPad). 我正在使用自动布局/大小类,以便仅对支持的各种设备使用一个情节提要(意思是:所有尺寸的iPhone和iPad)。

Noew everything is working, except one thing : the settings in "width compact x height compact" are used for 3"5 and 4" iPhones in landscape. Noew一切正常,只有一件事:“宽紧凑x高紧凑”中的设置用于横向3“ 5和4” iPhone。 So either I say that the table view takes 480px and there's a whole lot of space on iPhones 5/5S, either I set it at 480px but then I cannot reliably align items vertically in my cells. 因此,要么我说表格视图需要480px,而在iPhone 5 / 5S上有很多空间,要么我将其设置为480px,但是我不能可靠地在单元格中垂直对齐项目。

How come there is no way to say "this view should always takes the full width" ? 为何没有办法说“此视图应始终占据整个宽度”?

Any solution, even workaround, would do. 任何解决方案,甚至解决方法都可以。 Thanks a lot for your time ! 非常感谢您的宝贵时间!

There is no distinction in size classes between 3.5", 4" and 4.7" devices. They all have the same size classes deviced for horizontal and vertical, and behave the same way on rotation. 在3.5英寸,4英寸和4.7英寸设备之间,尺寸级别没有区别。它们在水平和垂直方向上都具有相同的尺寸级别,并且在旋转时的行为方式也相同。

You can define a view to take all the available space. 您可以定义一个视图以占用所有可用空间。 Explicitly set the bottom and top views to be attached to bottom and top edges (respectively) with constraints, and set the middle view (in your case, the table view) to take all the space in the middle by setting constraints on top with the top views, and constraints on bottom with the bottom views. 分别设置带有约束的底视图和顶视图,分别附加到底边缘和顶边缘,并通过在顶部设置约束,将中间视图(在您的情况下为表格视图)设置为占据中间的所有空间顶视图,底部约束与底视图。 Now, because top view is attached to top and bottom is attached to bottom, you will have the middle view grow and shrink as needed. 现在,由于顶视图连接到顶部,而底部连接到底部,您将使中间视图根据需要增长和缩小。

There are a couple of ways to get percentage based sizing. 有两种方法可以获取基于百分比的大小。

  1. Link the width of your target element to the width of another element. 将目标元素的宽度链接到另一个元素的宽度。
  2. Pin your element to its Superview and use the Multiplier setting instead of Constant . 将您的元素固定到其Superview并使用“ 乘数”设置而不是“ 常量”

Say you want to set a TitleLabel to have a relative width compared to another element. 假设您想将TitleLabel设置为与另一个元素相比具有相对宽度。

Example for method 1: 方法1的示例:

Ctrl + drag from TitleLabel to another Target element (such as its Parent View), then choose Equal Widths , you can then change the Multiplier field in your Constraints settings to 0.5 if you want 50% width. Ctrl +从TitleLabel拖动到另一个“目标”元素(例如其父视图),然后选择“ 相等宽度” ,然后,如果您想要50%的宽度,则可以将“约束”设置中的“ 乘数”字段更改为0.5。

Example for method 2: 方法2的示例:

Pin the appropriate edges of TitleLabel to either the Center, Bottom or Trailing edges* of its Superview (Editor -> Pin -> X Space to Superview). 将TitleLabel的适当边缘固定到其Superview的“ Center”,“ Bottom”或“ Trailing edge” *(编辑器-> Pin-> X Space to Superview)。

In the Constraints Settings, make sure you have TitleLabel in the First Item field, and the Superview as the Second Item . 在“约束设置”中,确保在“ 第一项”字段中具有TitleLabel,并将“ Superview”作为第二项

*because the Top/Leading edge of the Superview will have a 0 value, a multiplier against these won't work, as 0 x 0 = 0. *因为Superview的顶部/顶部边缘的值为0,所以乘以这些值的乘数将无效,因为0 x 0 = 0。

I found method #1 doesn't work well in Table Views, so you may have more luck with #2. 我发现方法#1在“表视图”中不能很好地工作,因此#2可能会带来更多的运气。

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

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