我第一次使用代码设置AutoLayout
视图的约束(我需要在不同的viewControllers中创建动态视图,我更喜欢在运行中创建这个UIView")。 / p>
我的UIView
应附加到superview top,与superview一样大,底部应附加到特定视图(导航栏)的顶部。
我尝试了以下内容:
let newView = UIView()
newView.backgroundColor = UIColor.red
view.addSubview(newView)
newView.translatesAutoresizingMaskIntoConstraints = false
let verticalConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0)
let widthConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: view,attribute: NSLayoutAttribute.width, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: navigationBar, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)
NSLayoutConstraint.activate([verticalConstraint, widthConstraint, bottomConstraint])
我收到以下运行时错误,但我无法理解什么是无用的约束:我有一个手动创建的视图具有相同的约束(还有一个更多)和我没有错误:
[LayoutConstraints] 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.
(
"<NSLayoutConstraint:0x60800009d060 UINavigationBar:0x7f8061c0d2b0.top == UILayoutGuide:0x6080001ace80'UIViewSafeAreaLayoutGuide'.top (active)>",
"<NSLayoutConstraint:0x60400008fc80 UIView:0x7f8061f13b20.centerY == UIView:0x7f8061c17ce0.centerY (active)>",
"<NSLayoutConstraint:0x60400008faa0 UIView:0x7f8061f13b20.bottom == UINavigationBar:0x7f8061c0d2b0.top (active)>",
"<NSLayoutConstraint:0x60000009b120 'UIView-Encapsulated-Layout-Height' UIView:0x7f8061c17ce0.height == 667 (active)>",
"<NSLayoutConstraint:0x60800009d0b0 'UIViewSafeAreaLayoutGuide-top' V:|-(20)-[UILayoutGuide:0x6080001ace80'UIViewSafeAreaLayoutGuide'] (active, names: '|':UIView:0x7f8061c17ce0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60400008fc80 UIView:0x7f8061f13b20.centerY == UIView:0x7f8061c17ce0.centerY (active)>
感谢任何帮助
答案 0 :(得分:1)
只需在自定义视图的超级视图中添加顶部,前导,尾随约束。 将底部约束添加到顶部另一个视图,该视图将位于自定义视图&amp;也是自定义视图的超级视图的子视图。
在您的代码中,问题也在于您的底部约束。您正尝试在自定义视图的底部添加约束b / w;导航栏顶部。
let topConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)
let leadingConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: anotherView, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)
答案 1 :(得分:0)
您不需要对centerY属性使用约束。由于您的目标是将视图固定到顶部,请尝试使用topAnchor约束并使其等于superview.topAnchor。希望这会有所帮助:
let newView = UIView()
newView.backgroundColor = UIColor.red
view.addSubview(newView)
newView.translatesAutoresizingMaskIntoConstraints = false
let topConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.topAnchor, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.topAnchor, multiplier: 1, constant: 0)
let widthConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: view,attribute: NSLayoutAttribute.width, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: navigationBar, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)
NSLayoutConstraint.activate([verticalConstraint, widthConstraint, bottomConstraint])