[英]Unable to simultaneously satisfy constraints when only one constraint is set by Visual Format Language
我创建了一个新项目来练习iOS AutoLayout功能( 可视格式语言)
我只做了一个按钮,如下
var button:UIButton!
我做了一个方法,它将按钮添加到self.view,命名为initViews,如下所示
func initViews() -> Void {
button = UIButton()
button.setTitle("Button", forState: .Normal)
button.backgroundColor = UIColor.blueColor()
self.view.addSubview(button)
self.createConstraints()
}
之后,我创建了另一个方法,如下所示用于设置约束。
func createConstraints () -> Void {
//Views to add constraints to
let views = Dictionary(dictionaryLiteral: ("button",button))
let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-[button]-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
self.view.addConstraints(horizontalConstraints)
// let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|-[button]-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
//self.view.addConstraints(verticalConstraints)
}
现在,我在ViewDidLoad中调用initViews,但是在执行时,它在LOG中发出警告。
我确定没有垂直约束我看不到按钮。 但我对警告更自觉。
警告:
2015-07-12 11:10:06.495 iOSAdvanceAutoLayout Project[1248:37226] 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:0x7fc9f062ffb0 UIButton:0x7fc9f061ccd0'Button'.leading == UIView:0x7fc9f0572cb0.leadingMargin>",
"<NSLayoutConstraint:0x7fc9f0630180 UIView:0x7fc9f0572cb0.trailingMargin == UIButton:0x7fc9f061ccd0'Button'.trailing>",
"<NSAutoresizingMaskLayoutConstraint:0x7fc9f056a820 h=--& v=--& H:[UIButton:0x7fc9f061ccd0'Button'(0)]>",
"<NSLayoutConstraint:0x7fc9f056d0e0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fc9f0572cb0(375)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fc9f0630180 UIView:0x7fc9f0572cb0.trailingMargin == UIButton:0x7fc9f061ccd0'Button'.trailing>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-07-12 11:10:06.496 iOSAdvanceAutoLayout Project[1248:37226] 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:0x7fc9f062ffb0 UIButton:0x7fc9f061ccd0'Button'.leading == UIView:0x7fc9f0572cb0.leadingMargin>",
"<NSAutoresizingMaskLayoutConstraint:0x7fc9f056c210 h=--& v=--& UIButton:0x7fc9f061ccd0'Button'.midX ==>",
"<NSAutoresizingMaskLayoutConstraint:0x7fc9f056a820 h=--& v=--& H:[UIButton:0x7fc9f061ccd0'Button'(0)]>",
"<NSAutoresizingMaskLayoutConstraint:0x7fc9f056d880 h=-&- v=-&- 'UIView-Encapsulated-Layout-Left' H:|-(0)-[UIView:0x7fc9f0572cb0] (Names: '|':UIWindow:0x7fc9f061b9d0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fc9f062ffb0 UIButton:0x7fc9f061ccd0'Button'.leading == UIView:0x7fc9f0572cb0.leadingMargin>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
谁能指导我为什么只设置一个约束就出现此警告?
谢谢
创建按钮后添加以下行
button.translatesAutoresizingMaskIntoConstraints = false
还添加垂直约束,如下编辑方法。
func createConstraints () -> Void {
//Views to add constraints to
let views = Dictionary(dictionaryLiteral: ("button",button))
let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-30.0-[button]-30.0-|",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: nil,
views: views)
self.view.addConstraints(horizontalConstraints)
let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|-30.0-[button]-30.0-|",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: nil,
views: views)
self.view.addConstraints(verticalConstraints)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.