我正在尝试在 uiview 的底边距和 ScrollView 的底边距之间设置编程约束。然而,xcode 给了我一些错误,文本看起来不合适,有时它根本不出现
所以基本上我有一个大小为 400 的 uiview 和一个 ScrollView 作为父 View 。 我正在尝试在 ScrollView 底部和 uiview 底部之间添加 300 的距离。我需要以编程方式执行此操作,因为无法判断在给定时间屏幕上会出现多少 View ,因此我需要以编程方式更改它们之间的约束。
我的代码是
view.addConstraint(NSLayoutConstraint(
item:container1,
attribute:NSLayoutAttribute.Bottom,
relatedBy:NSLayoutRelation.Equal,
toItem:scrollFrame,
attribute:NSLayoutAttribute.Bottom,
multiplier:0,
constant:400)
)
但它不起作用......显然,在模拟器上,uiview 位于距离顶部 400 处,或者当我增加常量时它向下移动(尝试使用 600)
Xcode 打印出以下错误:
2015-03-25 12:38:13.342 GraficTest[6612:707546] 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:0x7be906b0 V:|-(48)-[UIView:0x7be8e780] (Names: '|':UIScrollView:0x7be90340 )>",
"<NSLayoutConstraint:0x7bfd80e0 V:[UIView:0x7be8e780(257)]>",
"<NSLayoutConstraint:0x7bf6a940 UIView:0x7be8e780.bottom == + 600>"
)
你能帮帮我吗?
非常感谢!
最佳答案
从错误消息中我可以推断出一些事情:
- 将 UIView 放置在距 UIScrollView 顶部 48 点的位置有一个约束
- UIView 的高度限制为 257 点
- UIView 底部必须高于 UIScrollView 底部 600 点。
因为 UIScrollView 会有一些高度,假设是 X,从上面的限制来看,它应该是
X = 48 + 257 + 600 = 905
但显然情况并非如此,因此布局系统无法满足所有约束。
通过从 UIView 中删除高度限制,您可以解决此问题。
关于ios - Swift 编程约束底部边距不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29253350/