xcode 如何使用自动布局来缩放所有屏幕尺寸的视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26458858/
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
How to use autolayout to scale view for all screen sizes?
提问by ddevaz
The main navigation view of my app uses UIButtons with background images. I'm using autolayout in IB to lay out the view.
我的应用程序的主导航视图使用带有背景图像的 UIButton。我在 IB 中使用自动布局来布局视图。
I want the view to look the same (or similar, just scaled appropriately) on iphone 4, 5, 6, and 6plus. I have tried using vertical and horizontal constraints but have trouble figuring out how to use the aspect ratio constraint.
我希望视图在 iphone 4、5、6 和 6plus 上看起来相同(或相似,只是适当缩放)。我曾尝试使用垂直和水平约束,但无法弄清楚如何使用纵横比约束。
This is what the view looks like on an iphone 5.
这就是 iphone 5 上的视图。
Here is what I've got so far on an iphone 6
这是我目前在 iphone 6 上得到的
This is what I want it to look like on an iphone 6/6plus.
这就是我希望它在 iphone 6/6plus 上的样子。
How do I use autolayout to scale the view so that it looks the same or similar for all iphone screen sizes?
如何使用自动布局来缩放视图,使其对于所有 iphone 屏幕尺寸看起来都相同或相似?
回答by ttarik
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 (ORconstraints to give it the Y position you desire), Height Equals: [square height], Trailing Space to Superview, Leading Space to Superview.
首先,您需要创建一个容器(我的蓝色视图),它跨越父视图的整个宽度,并且高度等于或大于内部方块(橙色)。
此视图需要以下约束:将中心 X 对齐到超级视图,将中心 Y 对齐到超级视图(或约束为其提供所需的 Y 位置),高度等于:[正方形高度],尾随空间到超级视图,引导空间到超级视图。
The inner orange squares must be placed inside the container, and must be given the following constraints: Align Center X to Superview, LeadingSpace to Superview (value = space between square and edge of superview), TrailingSpace to [other view] (value = space between squares). Select the two squares together, and add Aspect Ratio, Equal Width and Equal Height constraints.
The items in bolddepend 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.
内部橙色方块必须放置在容器内,并且必须给出以下约束:将中心 X 对齐到超级视图,前导空间到超级视图(值 = 正方形和超级视图边缘之间的空间),尾随空间到 [其他视图](值= 方格之间的空间)。一起选择两个正方形,并添加纵横比、等宽和等高约束。粗体
项目取决于您选择的方块。左边的应该有超视图的前导空间和另一个视图的尾随空间,右边的应该交换这些约束。
This should give you your desired effect with autolayout.
这应该会给你你想要的自动布局效果。
回答by Tejas K
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. Add the top and left constraints.
2 - 现在选择第一个立方体,然后从屏幕底部选择 Pin 菜单。添加顶部和左侧约束。
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. Now select 'Equal Widths' from the menu. Confused huh ? Hold on.
4 - 现在选择第一个立方体,然后按住 cntrl 从立方体内部拖动到视图控制器。现在从菜单中选择“等宽”。困惑吧?坚持,稍等。
5 - Now double click on the 'Equal Widths' constraint line and a menu will pop up. In the 'Multiplier' field change the value to 0.4
5 - 现在双击“等宽”约束线,将弹出一个菜单。在“乘数”字段中将值更改为 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. (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). P.S: The bottoms are cropped because the screenshot is from preview :)
7 - 这就是结果在不同尺寸的 iPhone 中的样子。(第一个是 4 英寸设备,第二个是 4.7 英寸设备,第三个是 3.5 英寸设备)。PS:底部被裁剪,因为截图来自预览:)
UPDATE (16/01/2018)
更新 (16/01/2018)
In step 4, intead of selecting 'Equals Width', select 'Aspect Ratio'. Omit step 5 after this.
在步骤 4 中,不要选择“等宽”,而是选择“纵横比”。此后省略第 5 步。