我制作了一个新项目来练习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.
任何人都可以指导我为什么会出现这个警告而只设置了一个约束? 感谢
答案 0 :(得分:1)
创建按钮后添加以下行
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)
}