简体   繁体   English

如何使用自动布局在不同设备上维持UIView比率?

[英]How to maintain UIView ratio across different devices using Autolayout?

I have one UIView in LaunchScreen . 我在LaunchScreen有一个UIView

在此处输入图片说明

I have added following constraint to this UIView . 我向此UIView添加了以下约束。

在此处输入图片说明

I want my red view always 35% of the screen. 我希望我的红色视图始终35%屏幕的35%

I have calculated the height of RedView as : My ViewController height is 667 I calculated the 35% and assigned to RedView . 我将RedView的高度计算为:我的ViewController高度为667我计算了35%高度并分配给RedView

I want to know that is my approach is correct. 我想知道那是我的方法是正确的。 As their is very slight change in width of iPhone6Plus and huge change in height. 由于它们的iPhone6Plus宽度变化iPhone6Plus ,高度变化很大。

Is my constraint correct for 35:65 screen. 我的约束是否适合35:65屏幕。

Instead of giving Aspect ratio you could also give it a multiplier for 35%. 除了给出长宽比,您还可以给它35%的倍数。

Step 1: Pin RedView from 3 sides, Right-Top-Left 步骤1:从3个侧面固定RedViewRight-Top-Left

Step 2: Make RedView and ViewController's main view to Equal height. 步骤2:RedView和ViewController的主视图设置为相等的高度。

Step 3: Select RedView's equal height constraint and go to it's attribute inspector. 步骤3:选择RedView's等高约束,然后转到其属性检查器。 (4th right tab) (第4个右标签)

Step 4: Change Multiplier value to 0.35 . 步骤4:将Multiplier值更改为0.35

Now RedView will be always 35% of your main screen in both Orientations. 现在,在两个方向中,RedView始终将占主屏幕的35%

总是35%

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

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