【发布时间】:2012-10-02 03:55:01
【问题描述】:
我正在努力实现以下目标:
- 我的 xib 中有 2 个视图需要与边缘(两侧和顶部)保持 20 像素的距离
- 需要调整大小的 2 个视图大小不同
- 它们必须相距 20 像素
- 它们的宽度需要保持相对于父视图的宽度
我阅读了一个关于这样做的教程,它可以工作,但问题是它要求两个视图具有相同的宽度和 pin Widths equally,这是我不想要的。
这是我尝试过的:
- 向左视图添加前导空间约束为 20 像素
- 向左视图添加顶部空间约束为 20 像素
- 为右视图添加顶部空间约束为 20 像素
- 将拖尾空间约束添加到右视图为 20 像素
- 为两个视图添加水平间距约束为 20 像素
我遇到的问题是左视图没有调整大小,而右视图填充了空间以保持 20 像素的水平空间。
有没有办法让两个视图按比例调整它们应该填充的空间?
这是我的布局和约束的截图:
谢谢!
编辑
我在尝试旋转设备时收到以下警告:
2012-10-11 08:59:00.435 AutolayoutTest[35672:c07] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this: (1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it. (Note:
If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x8a6b2b0 H:[UIView:0x8a6b1d0(170)]>",
"<NSLayoutConstraint:0x8a68ad0 H:[UIView:0x8a69430(90)]>",
"<NSLayoutConstraint:0x8a6ba40 H:[UIView:0x8a69430]-(20)-[UIView:0x8a6b1d0]>",
"<NSLayoutConstraint:0x8a6ba00 H:[UIView:0x8a6b1d0]-(20)-| (Names: '|':UIView:0x8a6b7e0 )>",
"<NSLayoutConstraint:0x8a6b940 H:|-(20)-[UIView:0x8a69430] (Names: '|':UIView:0x8a6b7e0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x7199aa0 h=--& v=--& V:[UIView:0x8a6b7e0(568)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x8a6b2b0 H:[UIView:0x8a6b1d0(170)]>
【问题讨论】:
-
我也有同样的问题。这应该不是那么难。使用自动调整大小的蒙版非常容易。
-
XCode 6 中的一些更改使这变得容易多了。您可以为任何相等的宽度/高度约束设置“乘数”字段。或者您可以使用“纵横比”约束来完成此操作。下面的许多答案(不是公认的)解释了这一点
标签: ios interface-builder ios6 autolayout nslayoutconstraint