草庐IT

ios - 以编程方式添加约束

coder 2023-09-17 原文

我想以编程方式将 5 个按钮放置在屏幕上。

我希望它们在 View 底部上方 17 像素,每个按钮之间有 20 像素的空间。

我是这样做的:

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.

最佳答案

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))

关于ios - 以编程方式添加约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38992560/

有关ios - 以编程方式添加约束的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby - 如何以所有可能的方式将字符串拆分为长度最多为 3 的连续子字符串? - 2

    我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123

  3. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  4. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  5. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  6. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  7. ruby-on-rails - 正确的 Rails 2.1 做事方式 - 2

    question的一些答案关于redirect_to让我想到了其他一些问题。基本上,我正在使用Rails2.1编写博客应用程序。我一直在尝试自己完成大部分工作(因为我对Rails有所了解),但在需要时会引用Internet上的教程和引用资料。我设法让一个简单的博客正常运行,然后我尝试添加评论。靠我自己,我设法让它进入了可以从script/console添加评论的阶段,但我无法让表单正常工作。我遵循的其中一个教程建议在帖子Controller中创建一个“评论”操作,以添加评论。我的问题是:这是“标准”方式吗?我的另一个问题的答案之一似乎暗示应该有一个CommentsController参

  8. ruby - 寻找通过阅读代码确定编程语言的ruby gem? - 2

    几个月前,我读了一篇关于ruby​​gem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:

  9. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  10. ruby - 可以通过多少种方法将方法添加到 ruby​​ 对象? - 2

    当谈到运行时自省(introspection)和动态代码生成时,我认为ruby​​没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby​​的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资

随机推荐