ios 使用自动布局将屏幕分为 1/3 和 2/3 视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31261546/
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
Dividing screen in to 1/3 and 2/3 views using autolayout
提问by ilan
Is there a way to divide the screen by percentage (not equal percentage) I need to split the screen to two parts 1/3 and 2/3. I know how to split to equal parts but cant figure out how to split to non equal.
有没有办法按百分比(不等百分比)划分屏幕我需要将屏幕分成两部分 1/3 和 2/3。我知道如何拆分为相等的部分,但无法弄清楚如何拆分为不相等的部分。
Thanks
谢谢
回答by Stefan
Make an "Equal widths" constraints between your view and the super view with multiplier 2:3 for one view and 1:3 for the other. See picture. "Equal height" if you want to split in the other direction.
在您的视图和超级视图之间设置“等宽”约束,一个视图乘数为 2:3,另一个视图乘数为 1:3。见图片。如果要向另一个方向拆分,则为“等高”。
回答by ilan
Steps :
脚步 :
- Drag a Two
UIView objects
onviewcontroller
. - Pin left-view to left, top and bottom of
superview
- And Right-view to right, top and bottom of
superview
. - Pin the horizontal spacing between two views(left and right). Make sure constant is zero since/if you want no spacing.
- Select left-view and superview and set width equally (hold your patience). Now go to size inspector and edit width equally constraint having left view selected. Now set the multiplier to 0.33 (now % width is set).
- 拖动两个
UIView objects
上viewcontroller
。 - 将左视图固定到左侧、顶部和底部
superview
- 和 右视图的右侧,顶部和底部
superview
。 - 固定两个视图(左和右)之间的水平间距。确保常量为零,因为/如果你想要没有间距。
- 选择 left-view 和 superview 并设置宽度相等(耐心等待)。现在转到尺寸检查器并在选择左视图的情况下编辑宽度相等的约束。现在将乘数设置为 0.33(现在设置了 % 宽度)。
See the result :
查看结果:
回答by Massimo Polimeni
Using storyboard & autolayout it's very easy: I suppose you need to split the screen vertically (but horizontally it's the same trick).
使用情节提要和自动布局非常简单:我想您需要垂直拆分屏幕(但水平拆分是相同的技巧)。
So using a single view controller, drag&drop two views and set them attached to top and bottom. For the the view 1:
因此,使用单个视图控制器,拖放两个视图并将它们设置为附加到顶部和底部。对于视图 1:
view 2:
视图2:
Now you need to set the constraint equal heightfor the view 1 among the main view and the same for the view 2. After that you can edit these constraints settings a value (between 0 and 1) as multiplier. Double click on the constraint:
现在您需要为主视图中的视图 1 和视图 2设置相同高度的约束。之后,您可以编辑这些约束设置一个值(在 0 和 1 之间)作为乘数。双击约束:
and set the value that you want for proportion (the trick it's maintain the sum of these constraints as 1 or just put in "2:3" here and "1:3" in the other view):
并设置您想要的比例值(技巧是将这些约束的总和保持为 1,或者只是在此处输入“2:3”而在另一个视图中输入“1:3”):
So this is the result:
所以这是结果:
I hope this is what you need, tell me if you need more details!
我希望这是您需要的,如果您需要更多详细信息,请告诉我!
回答by shoe
You can use the Equal Widths
constraint as in the accepted answer, however, if what you're trying to constrain is not two sizes but rather one point to another, such as constraining one view's horizontal center
to the 1/3 or 2/3's point on the screen, then it won't work because you can't constrain a point in space to a size.
您可以使用已Equal Widths
接受的答案中的约束,但是,如果您要约束的不是两种尺寸而是一个指向另一个,例如将一个视图约束horizontal center
到屏幕上的 1/3 或 2/3 点,那么它将不起作用,因为您无法将空间中的点限制为某个大小。
But if you know say, view1
, in your xib
is going to be the full width of the screen, then, for example with the horizontal center
point, you can constrain it to that view's trailing
edge, and if you want the view to be centered at 1/3 or 2/3's the screen's width then set the multiplier to 1:3
or 2:3
, respectively.
但是,如果您知道说,view1
在您xib
将是屏幕的整个宽度,那么,例如使用horizontal center
点,您可以将其限制在该视图的trailing
边缘,并且如果您希望视图以 1/3 为中心或 2/3 的屏幕宽度,然后将乘数分别设置为1:3
或2:3
。