简体   繁体   English

如何使用autolayout缩放所有屏幕尺寸的视图?

[英]How to use autolayout to scale view for all screen sizes?

The main navigation view of my app uses UIButtons with background images. 我的应用程序的主导航视图使用UIButtons和背景图像。 I'm using autolayout in IB to lay out the view. 我在IB中使用autolayout来布局视图。

I want the view to look the same (or similar, just scaled appropriately) on iphone 4, 5, 6, and 6plus. 我希望iphone 4,5,6和6plus上的视图看起来相同(或类似,只是适当缩放)。 I have tried using vertical and horizontal constraints but have trouble figuring out how to use the aspect ratio constraint. 我尝试过使用纵向和横向约束,但却无法弄清楚如何使用纵横比约束。

This is what the view looks like on an iphone 5. 这就是iphone 5上的视图。

在iphone5上查看

Here is what I've got so far on an iphone 6 这是我到目前为止在iphone 6上得到的

在iphone6上查看

This is what I want it to look like on an iphone 6/6plus. 这就是我想在iphone 6 / 6plus上看起来的样子。

在iphone6上查看 - 正确布局

How do I use autolayout to scale the view so that it looks the same or similar for all iphone screen sizes? 如何使用autolayout缩放视图,使其在所有iphone屏幕尺寸上看起来相同或相似?

I've done some tinkering and I believe I've found a solution. 我做了一些修补,我相信我找到了解决方案。 I will demonstrate how to do it for the two larger squares roughly in the middle of your view. 我将演示如何在视图中间大致为两个较大的正方形做这个。

Here is a reference image: 这是一个参考图像:

在此输入图像描述

First, you need to create a container (my blue view) that spans the entire width of the superview, and has a height the same or larger than your inner squares (orange). 首先,您需要创建一个横跨超视图整个宽度的容器(我的蓝色视图),其高度与内部正方形(橙色)相同或更大。
This view needs the following constraints: Align Center X to Superview, Align Center Y to Superview ( OR constraints to give it the Y position you desire), Height Equals: [square height], Trailing Space to Superview, Leading Space to Superview. 此视图需要以下约束:将中心X与Superview对齐,将中心Y与Superview对齐( OR约束以使其为您想要的Y位置),高度等于:[方形高度],尾随空间到超级视图,前导空间到超级视图。

The inner orange squares must be placed inside the container, and must be given the following constraints: Align Center X to Superview, Leading Space to Superview (value = space between square and edge of superview), Trailing Space to [other view] (value = space between squares). 内橙色正方形必须放在容器内,并且必须给出以下约束:将中心X与超视图对齐,将前导空间与超视图对齐(值=正方形与超视图边缘之间的空间), 尾随空格到[其他视图](值=正方形之间的空间)。 Select the two squares together, and add Aspect Ratio, Equal Width and Equal Height constraints. 选择两个方块,并添加“宽高比”,“等宽”和“等高”约束。
The items in bold depend on which square you're selecting. 粗体项取决于您选择的方格。 The left one should have leading space to superview and trailing space to the other view, and the right one should have these constraints swapped. 左边的应该有超前视图的前导空格和其他视图的尾随空间,右边的应该交换这些约束。

This should give you your desired effect with autolayout. 这可以通过自动布局为您提供所需的效果。

A little late to the party but I guess this will help someone in the future. 派对有点晚,但我猜这将有助于将来的某个人。 Here is what I think is a better way to do it. 这是我认为更好的方法。 I will write the steps I followed to achieve the desired result. 我将编写我遵循的步骤以达到预期的结果。

1 - Add both the squares in the view controller as below. 1 - 在视图控制器中添加两个方块,如下所示。

在此输入图像描述

2 - Now select the first cube, and select the Pin menu from the bottom of the screen. 2 - 现在选择第一个立方体,然后从屏幕底部选择Pin菜单。 Add the top and left constraints. 添加顶部和左侧约束。

3 - Now select the second cube, the one on the right and add right and top constraints to it from the Pin menu. 3 - 现在选择第二个立方体,右边的立方体,并从Pin菜单中添加右边和上边的约束。

4 - Now select the first cube, and cntrl-drag from inside of the cube to the view controller. 4 - 现在选择第一个立方体,然后从立方体内部cntrl-拖动到视图控制器。 Now select 'Equal Widths' from the menu. 现在从菜单中选择“Equal Widths”。 Confused huh ? 困惑吧? Hold on. 坚持,稍等。 在此输入图像描述

5 - Now double click on the 'Equal Widths' constraint line and a menu will pop up. 5 - 现在双击“Equal Widths”约束线,弹出一个菜单。 In the 'Multiplier' field change the value to 0.4 在“乘数”字段中,将值更改为0.4

6 - Repeat step 4 and 5 for the right cube. 6 - 对右侧立方体重复步骤4和5。

7 - This is how the result will look like in different sizes of iPhone. 7 - 这就是不同尺寸iPhone的结果。 (The first one is a 4-inch device, the second is a 4.7-inch device and the third is a 3.5-inch device). (第一个是4英寸设备,第二个是4.7英寸设备,第三个是3.5英寸设备)。 PS: The bottoms are cropped because the screenshot is from preview :) PS:底部被裁剪,因为截图来自预览:)

在此输入图像描述

UPDATE (16/01/2018) 更新(16/01/2018)

In step 4, intead of selecting 'Equals Width', select 'Aspect Ratio'. 在步骤4中,选择'Equals Width',选择'Aspect Ratio'。 Omit step 5 after this. 在此之后省略步骤5。

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

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