2015-07-12 55 views
1

我做了一个新的项目,以实践的iOS自动版式功能,视觉格式语言无法在只有一个约束是通过视觉格式语言设置为同时满足约束条件

我只取得了一个按钮,如下

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,但因为它执行,它给在日志警告。

我相信我不能看到没有垂直约束的按钮。但我更关注警告。

警告:

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. 

任何人都可以指导我,为什么这个警告是有,而只有一个约束条件设置?
感谢

回答

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) 
    } 
相关问题