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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:45:46  来源:igfitidea点击:

Dividing screen in to 1/3 and 2/3 views using autolayout

iosswiftios-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

Xcode contraints

Xcode 限制

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 :

脚步 :

  1. Drag a Two UIView objectson viewcontroller.
  2. Pin left-view to left, top and bottom of superview
  3. And Right-view to right, top and bottom of superview.
  4. Pin the horizontal spacing between two views(left and right). Make sure constant is zero since/if you want no spacing.
  5. 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).
  1. 拖动两个UIView objectsviewcontroller
  2. 将左视图固定到左侧、顶部和底部 superview
  3. 和 右视图的右侧,顶部和底部superview
  4. 固定两个视图(左和右)之间的水平间距。确保常量为零,因为/如果你想要没有间距。
  5. 选择 left-view 和 superview 并设置宽度相等(耐心等待)。现在转到尺寸检查器并在选择左视图的情况下编辑宽度相等的约束。现在将乘数设置为 0.33(现在设置了 % 宽度)。

enter image description here

在此处输入图片说明

See the result :

查看结果:

enter image description here

在此处输入图片说明

回答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 Widthsconstraint 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 centerto 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 xibis going to be the full width of the screen, then, for example with the horizontal centerpoint, you can constrain it to that view's trailingedge, 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:3or 2:3, respectively.

但是,如果您知道说,view1在您xib将是屏幕的整个宽度,那么,例如使用horizontal center点,您可以将其限制在该视图的trailing边缘,并且如果您希望视图以 1/3 为中心或 2/3 的屏幕宽度,然后将乘数分别设置为1:32:3