2015-07-12 54 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) 
    } 
相關問題