当视图框架为零时无法同时满足约束
Unable to simultaneously satisfy constraints when view frame is zero
我创建视图
init(){
super.init(frame: .zero)
}
创建视图后我想设置我的子视图
func setViews(){
self.addSubview(labelView)
self.addConstraintsWithFormat("V:|-10-[v0]-10-|", views: labelView)
self.addConstraintsWithFormat("H:|-10-[v0]-10-|", views: labelView)
}
一切正常,但我有警告
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:0x7f87ea8e6520 V:|-(10)-[BIPartners.labelView:0x7f87eb56be20] (Names: '|':BIPartners.labelView:0x7f87eb56a320 )>",
"<NSLayoutConstraint:0x7f87ea8e6af0 V:[BIPartners.labelView:0x7f87eb56be20]-(10)-| (Names: '|':BIPartners.labelView:0x7f87eb56a320 )>",
"<NSLayoutConstraint:0x7f87e868bdc0 '_UITemporaryLayoutHeight' V:[BIPartners.labelView:0x7f87eb56a320(0)]>"
)
我知道问题出在我将帧设置为零时。我将框架设置为零,并将垂直边距设置为 10。我应该如何解决这个问题?
使用 WTF Auto Layout 了解您的错误。
在设置约束之前检查视图的 bounds
。如果等于 CGRect.zero
,则不应用约束。
您应该通过将标签底部固定约束的优先级降低到小于 1000 来解决此问题:
self.addConstraintsWithFormat("V:|-10-[v0]-10(@750)-|", views: labelView)
这将消除您的约束的歧义,保留您的底部填充和标签的固有高度。
基本上,当您想要使用固有内容大小时并将标签固定在两侧时,就会出现这种歧义。
当然,还有其他方法可以获得所需的间距(边距),但这个答案最接近你想要做的。
我创建视图
init(){
super.init(frame: .zero)
}
创建视图后我想设置我的子视图
func setViews(){
self.addSubview(labelView)
self.addConstraintsWithFormat("V:|-10-[v0]-10-|", views: labelView)
self.addConstraintsWithFormat("H:|-10-[v0]-10-|", views: labelView)
}
一切正常,但我有警告
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:0x7f87ea8e6520 V:|-(10)-[BIPartners.labelView:0x7f87eb56be20] (Names: '|':BIPartners.labelView:0x7f87eb56a320 )>",
"<NSLayoutConstraint:0x7f87ea8e6af0 V:[BIPartners.labelView:0x7f87eb56be20]-(10)-| (Names: '|':BIPartners.labelView:0x7f87eb56a320 )>",
"<NSLayoutConstraint:0x7f87e868bdc0 '_UITemporaryLayoutHeight' V:[BIPartners.labelView:0x7f87eb56a320(0)]>"
)
我知道问题出在我将帧设置为零时。我将框架设置为零,并将垂直边距设置为 10。我应该如何解决这个问题?
使用 WTF Auto Layout 了解您的错误。
在设置约束之前检查视图的 bounds
。如果等于 CGRect.zero
,则不应用约束。
您应该通过将标签底部固定约束的优先级降低到小于 1000 来解决此问题:
self.addConstraintsWithFormat("V:|-10-[v0]-10(@750)-|", views: labelView)
这将消除您的约束的歧义,保留您的底部填充和标签的固有高度。
基本上,当您想要使用固有内容大小时并将标签固定在两侧时,就会出现这种歧义。
当然,还有其他方法可以获得所需的间距(边距),但这个答案最接近你想要做的。