2016-08-17 137 views
2

我有5個按鈕,我想以編程方式放置在屏幕上。以編程方式添加約束條件

我希望它們在視圖底部以上17px,每個按鈕之間有20px的空間。

我已經做了這樣的:

let button1 = UIButton(frame: CGRectMake(20, 48, 50, 50)) 
button1.setImage(UIImage(named: "button1"), forState: .Normal) 

let button2 = UIButton(frame: CGRectMake(91, 48, 50, 50)) 
button2.setImage(UIImage(named: "button2"), forState: .Normal) 

let button3 = UIButton(frame: CGRectMake(162, 48, 50, 50)) 
button3.setImage(UIImage(named: "button3"), forState: .Normal) 

let button4 = UIButton(frame: CGRectMake(233, 48, 50, 50)) 
     button4.setImage(UIImage(named: "button4"), forState: .Normal) 

let button5 = UIButton(frame: CGRectMake(304, 48, 50, 50)) 
button5.setImage(UIImage(named: "button5"), forState: .Normal) 

self.addSubview(button1) 
self.addSubview(button2) 
self.addSubview(button3) 
self.addSubview(button4) 
self.addSubview(button5) 

我的偉大工程,正是我想要的!但僅僅在iPhone 6s的屏幕尺寸上,所以我想讓它適用於所有屏幕尺寸的方法是使用約束,問題是我不知道如何以編程方式添加約束。

有人可以幫我嗎?

謝謝!

SaintThread答案,記錄錯誤:

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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7d968a10 h=--& v=--& UIButton:0x79729670.midX == + 45>", 
    "<NSLayoutConstraint:0x79725610 H:|-(48)-[UIButton:0x79729670] (Names: '|':Arounder.CategoriesBarView:0x797c7190)>", 
    "<NSLayoutConstraint:0x797255e0 H:[UIButton:0x79729670(50)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x797255e0 H:[UIButton:0x79729670(50)]> 

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. 
2016-08-17 12:49:14.305 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7d968a10 h=--& v=--& UIButton:0x79729670.midX == + 45>", 
    "<NSLayoutConstraint:0x79725610 H:|-(48)-[UIButton:0x79729670] (Names: '|':Arounder.CategoriesBarView:0x797c7190)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x79725610 H:|-(48)-[UIButton:0x79729670] (Names: '|':Arounder.CategoriesBarView:0x797c7190)> 

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. 
2016-08-17 12:49:14.305 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7d96a3c0 h=--& v=--& UIButton:0x79729670.midY == + 73>", 
    "<NSLayoutConstraint:0x79725c50 V:|-(20)-[UIButton:0x79729670] (Names: '|':Arounder.CategoriesBarView:0x797c7190)>", 
    "<NSLayoutConstraint:0x79725ad0 V:[UIButton:0x79729670(50)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x79725ad0 V:[UIButton:0x79729670(50)]> 

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. 
2016-08-17 12:49:14.306 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7d96a3c0 h=--& v=--& UIButton:0x79729670.midY == + 73>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7d96a3f0 h=--& v=--& V:[UIButton:0x79729670(50)]>", 
    "<NSLayoutConstraint:0x79725c50 V:|-(20)-[UIButton:0x79729670] (Names: '|':Arounder.CategoriesBarView:0x797c7190)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x79725c50 V:|-(20)-[UIButton:0x79729670] (Names: '|':Arounder.CategoriesBarView:0x797c7190)> 

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. 
2016-08-17 12:49:14.306 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7a991c60 h=--& v=--& UIButton:0x79729080.midX == + 116>", 
    "<NSLayoutConstraint:0x79725560 H:|-(48)-[UIButton:0x79729080] (Names: '|':Arounder.CategoriesBarView:0x797c7190)>", 
    "<NSLayoutConstraint:0x79725510 H:[UIButton:0x79729080(50)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x79725510 H:[UIButton:0x79729080(50)]> 

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. 
2016-08-17 12:49:14.306 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7a991c60 h=--& v=--& UIButton:0x79729080.midX == + 116>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7a991c90 h=--& v=--& H:[UIButton:0x79729080(50)]>", 
    "<NSLayoutConstraint:0x79725560 H:|-(48)-[UIButton:0x79729080] (Names: '|':Arounder.CategoriesBarView:0x797c7190)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x79725560 H:|-(48)-[UIButton:0x79729080] (Names: '|':Arounder.CategoriesBarView:0x797c7190)> 

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. 
2016-08-17 12:49:14.307 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7d96a3c0 h=--& v=--& UIButton:0x79729670.midY == + 73>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7d96a3f0 h=--& v=--& V:[UIButton:0x79729670(50)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7a991cc0 h=--& v=--& UIButton:0x79729080.midY == + 73>", 
    "<NSLayoutConstraint:0x79725a10 V:[UIButton:0x79729670]-(20)-[UIButton:0x79729080]>", 
    "<NSLayoutConstraint:0x797259e0 V:[UIButton:0x79729080(50)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x797259e0 V:[UIButton:0x79729080(50)]> 

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. 
2016-08-17 12:49:14.307 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7d96a3c0 h=--& v=--& UIButton:0x79729670.midY == + 73>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7d96a3f0 h=--& v=--& V:[UIButton:0x79729670(50)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7a991cc0 h=--& v=--& UIButton:0x79729080.midY == + 73>", 
    "<NSLayoutConstraint:0x79725a10 V:[UIButton:0x79729670]-(20)-[UIButton:0x79729080]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x79725a10 V:[UIButton:0x79729670]-(20)-[UIButton:0x79729080]> 

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. 
2016-08-17 12:49:14.307 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7d96b720 h=--& v=--& UIButton:0x79728480.midX == + 187>", 
    "<NSLayoutConstraint:0x79725490 H:|-(48)-[UIButton:0x79728480] (Names: '|':Arounder.CategoriesBarView:0x797c7190)>", 
    "<NSLayoutConstraint:0x79725440 H:[UIButton:0x79728480(50)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x79725440 H:[UIButton:0x79728480(50)]> 

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. 
2016-08-17 12:49:14.308 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7d96b720 h=--& v=--& UIButton:0x79728480.midX == + 187>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7d96b750 h=--& v=--& H:[UIButton:0x79728480(50)]>", 
    "<NSLayoutConstraint:0x79725490 H:|-(48)-[UIButton:0x79728480] (Names: '|':Arounder.CategoriesBarView:0x797c7190)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x79725490 H:|-(48)-[UIButton:0x79728480] (Names: '|':Arounder.CategoriesBarView:0x797c7190)> 

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. 
2016-08-17 12:49:14.308 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7a991cc0 h=--& v=--& UIButton:0x79729080.midY == + 73>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7a991cf0 h=--& v=--& V:[UIButton:0x79729080(50)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7d96c260 h=--& v=--& UIButton:0x79728480.midY == + 73>", 
    "<NSLayoutConstraint:0x797259b0 V:[UIButton:0x79729080]-(20)-[UIButton:0x79728480]>", 
    "<NSLayoutConstraint:0x79725980 V:[UIButton:0x79728480(50)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x79725980 V:[UIButton:0x79728480(50)]> 

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. 
2016-08-17 12:49:14.308 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7a991cc0 h=--& v=--& UIButton:0x79729080.midY == + 73>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7a991cf0 h=--& v=--& V:[UIButton:0x79729080(50)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7d96c260 h=--& v=--& UIButton:0x79728480.midY == + 73>", 
    "<NSLayoutConstraint:0x797259b0 V:[UIButton:0x79729080]-(20)-[UIButton:0x79728480]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x797259b0 V:[UIButton:0x79729080]-(20)-[UIButton:0x79728480]> 

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. 
2016-08-17 12:49:14.309 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7aad76b0 h=--& v=--& UIButton:0x79727bc0.midX == + 258>", 
    "<NSLayoutConstraint:0x797253b0 H:|-(48)-[UIButton:0x79727bc0] (Names: '|':Arounder.CategoriesBarView:0x797c7190)>", 
    "<NSLayoutConstraint:0x79725380 H:[UIButton:0x79727bc0(50)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x79725380 H:[UIButton:0x79727bc0(50)]> 

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. 
2016-08-17 12:49:14.309 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7aad76b0 h=--& v=--& UIButton:0x79727bc0.midX == + 258>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7aad76e0 h=--& v=--& H:[UIButton:0x79727bc0(50)]>", 
    "<NSLayoutConstraint:0x797253b0 H:|-(48)-[UIButton:0x79727bc0] (Names: '|':Arounder.CategoriesBarView:0x797c7190)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x797253b0 H:|-(48)-[UIButton:0x79727bc0] (Names: '|':Arounder.CategoriesBarView:0x797c7190)> 

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. 
2016-08-17 12:49:14.309 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7d96c260 h=--& v=--& UIButton:0x79728480.midY == + 73>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7d96c290 h=--& v=--& V:[UIButton:0x79728480(50)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7aad7710 h=--& v=--& UIButton:0x79727bc0.midY == + 73>", 
    "<NSLayoutConstraint:0x79725950 V:[UIButton:0x79728480]-(20)-[UIButton:0x79727bc0]>", 
    "<NSLayoutConstraint:0x79725920 V:[UIButton:0x79727bc0(50)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x79725920 V:[UIButton:0x79727bc0(50)]> 

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. 
2016-08-17 12:49:14.309 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7d96c260 h=--& v=--& UIButton:0x79728480.midY == + 73>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7d96c290 h=--& v=--& V:[UIButton:0x79728480(50)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7aad7710 h=--& v=--& UIButton:0x79727bc0.midY == + 73>", 
    "<NSLayoutConstraint:0x79725950 V:[UIButton:0x79728480]-(20)-[UIButton:0x79727bc0]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x79725950 V:[UIButton:0x79728480]-(20)-[UIButton:0x79727bc0]> 

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. 
2016-08-17 12:49:14.310 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7a993980 h=--& v=--& UIButton:0x797270c0.midX == + 329>", 
    "<NSLayoutConstraint:0x797252e0 H:|-(48)-[UIButton:0x797270c0] (Names: '|':Arounder.CategoriesBarView:0x797c7190)>", 
    "<NSLayoutConstraint:0x79725290 H:[UIButton:0x797270c0(50)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x79725290 H:[UIButton:0x797270c0(50)]> 

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. 
2016-08-17 12:49:14.310 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7a993980 h=--& v=--& UIButton:0x797270c0.midX == + 329>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7a9939b0 h=--& v=--& H:[UIButton:0x797270c0(50)]>", 
    "<NSLayoutConstraint:0x797252e0 H:|-(48)-[UIButton:0x797270c0] (Names: '|':Arounder.CategoriesBarView:0x797c7190)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x797252e0 H:|-(48)-[UIButton:0x797270c0] (Names: '|':Arounder.CategoriesBarView:0x797c7190)> 

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. 
2016-08-17 12:49:14.310 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7aad7710 h=--& v=--& UIButton:0x79727bc0.midY == + 73>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7aad7a50 h=--& v=--& V:[UIButton:0x79727bc0(50)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7a993510 h=--& v=--& UIButton:0x797270c0.midY == + 73>", 
    "<NSLayoutConstraint:0x797258d0 V:[UIButton:0x79727bc0]-(20)-[UIButton:0x797270c0]>", 
    "<NSLayoutConstraint:0x797258a0 V:[UIButton:0x797270c0(50)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x797258a0 V:[UIButton:0x797270c0(50)]> 

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. 
2016-08-17 12:49:14.311 Arounder[3992:10988727] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7aad7710 h=--& v=--& UIButton:0x79727bc0.midY == + 73>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7aad7a50 h=--& v=--& V:[UIButton:0x79727bc0(50)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x7a993510 h=--& v=--& UIButton:0x797270c0.midY == + 73>", 
    "<NSLayoutConstraint:0x797258d0 V:[UIButton:0x79727bc0]-(20)-[UIButton:0x797270c0]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x797258d0 V:[UIButton:0x79727bc0]-(20)-[UIButton:0x797270c0]> 

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

iOS 8的安全

添加的按鈕之前,以查看

button1.translatesAutoresizingMaskIntoConstraints = false 
button2.translatesAutoresizingMaskIntoConstraints = false 
button3.translatesAutoresizingMaskIntoConstraints = false 
button4.translatesAutoresizingMaskIntoConstraints = false 
button5.translatesAutoresizingMaskIntoConstraints = false 

添加按鈕後,查看

let views = ["button1": button1, 
    "button2": button2, 
    "button3": button3, 
    "button4": button4, 
    "button5": button5] 

let buttonConstraintsH = NSLayoutConstraint.constraintsWithVisualFormat(
    "H:|-20-[button1]-20-[button2]-20-[button3]-20-[button4]-20-[button5]-20-|", 
    options: [], 
    metrics: nil, 
    views: views) 

let button1ConstraintsV = NSLayoutConstraint.constraintsWithVisualFormat(
    "V:[button1]-17-|", 
    options: [], 
    metrics: nil, 
    views: views) 

let button2ConstraintsV = NSLayoutConstraint.constraintsWithVisualFormat(
    "V:[button2]-17-|", 
    options: [], 
    metrics: nil, 
    views: views) 

let button3ConstraintsV = NSLayoutConstraint.constraintsWithVisualFormat(
    "V:[button3]-17-|", 
    options: [], 
    metrics: nil, 
    views: views) 

let button4ConstraintsV = NSLayoutConstraint.constraintsWithVisualFormat(
    "V:[button4]-17-|", 
    options: [], 
    metrics: nil, 
    views: views) 

let button5ConstraintsV = NSLayoutConstraint.constraintsWithVisualFormat(
    "V:[button5]-17-|", 
    options: [], 
    metrics: nil, 
    views: views) 

NSLayoutConstraint.activateConstraints([buttonConstraintsH, button1ConstraintsV, button2ConstraintsV, button3ConstraintsV, button4ConstraintsV, button5ConstraintsV]) 

,並具有相同的比例(寫爲每個按鈕):

NSLayoutConstraint.activateConstraints(NSLayoutConstraint( 
item:button1 
attribute:NSLayoutAttributeHeight 
relatedBy:NSLayoutRelationEqual 
toItem:button1 
attribute:NSLayoutAttributeWidth 
multiplier:1 
constant:0)) 
+0

這不起作用,圖標真的很大,我希望它也調整圖標的大小 –

+0

@ FS.O5您是否在使用圖像或背景圖像作爲按鈕?我的代碼是50 x 50像素嗎? –

+0

是的,他們是。我正在使用圖像 –

0

我建議你閱讀更多關於NSLayoutAnchor(read more),這是建立約束更簡單的方法。

短片斷:

self.add

self.addSubview(button1) 
self.addSubview(button2) 
self.addSubview(button3) 
self.addSubview(button4) 
self.addSubview(button5) 

button1.widthAnchor.constraintEqualToConstant(50).active = true 
button1.heightAnchor.constraintEqualToConstant(50).active = true 
button1.leadingAnchor.constraintEqualToAnchor(self.leadingAnchor, constant: 20).active = true 
button1.topAnchor.constraintEqualToAnchor(self.topAnchor, constant: 50).active = true 
.... 
+0

這並不工作,該圖標是非常大的,我想它調整圖標的大小 –

相關問題