当使用具有固定宽度和高度的 ImageView 在内部添加两个视图时,堆栈视图约束会中断



>我有一个带有图像视图和标签的堆栈视图,但是当我尝试将其添加到堆栈视图时,它没有出现,显然所有约束都中断了。

let userView_stackView = UIStackView()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
userView_stackView.axis = .vertical
userView_stackView.alignment = .center
userView_stackView.spacing = 5
userView_stackView.addArrangedSubview(user_imageView)
userView_stackView.addArrangedSubview(user_name)
self.contentView.addSubview(userView_stackView)
stackView_constraints()
}
var user_imageView: UIImageView = {
let imageView = UIImageView()
let imageViewHeightAndWidth: CGFloat = 60
let image = UIImage(named: "steve")
imageView.image = image
imageView.layer.borderWidth = 1
imageView.clipsToBounds = true
imageView.layer.borderColor = UIColor.white.cgColor
imageView.layer.cornerRadius = imageViewHeightAndWidth / 2
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.heightAnchor.constraint(equalToConstant: imageViewHeightAndWidth).isActive = true
imageView.widthAnchor.constraint(equalToConstant: imageViewHeightAndWidth).isActive = true
return imageView
}()
var user_name: UILabel = {
let label = UILabel()
label.text = "Steve Rodriquez"
label.font = label.font.withSize(14)
return label
}()
func stackView_constraints() {
userView_stackView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 0).isActive =
true
userView_stackView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 0)
.isActive = true
userView_stackView.trailingAnchor.constraint(
equalTo: self.contentView.trailingAnchor, constant: 0
).isActive = true
userView_stackView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 0)
.isActive = true
}

添加这样的视图会破坏约束,并在控制台上打印以下错误,其中所有约束都中断了

[LayoutConstraints] 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. 
(
"<NSLayoutConstraint:0x280d70eb0 UIStackView:0x10327c250.height == 283   (active)>",
"<NSLayoutConstraint:0x280d72580 'UIView-Encapsulated-Layout-Height' UIStackView:0x10327c250.height == 0   (active)>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x280d70eb0 UIStackView:0x10327c250.height == 283   (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2019-10-12 10:43:30.597559-0500 Fourmi[1708:291157] [LayoutConstraints] 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. 
(
"<NSLayoutConstraint:0x280d5f610 UIImageView:0x10327c6d0.width == 60   (active)>",
"<NSLayoutConstraint:0x280d72c10 'UISV-canvas-connection' UIStackView:0x10327c250.leading == _UILayoutSpacer:0x281174960'UISV-alignment-spanner'.leading   (active)>",
"<NSLayoutConstraint:0x280d73020 'UISV-canvas-connection' UIStackView:0x10327c250.centerX == UIImageView:0x10327c6d0.centerX   (active)>",
"<NSLayoutConstraint:0x280d72d50 'UISV-spanning-boundary' _UILayoutSpacer:0x281174960'UISV-alignment-spanner'.leading <= UIImageView:0x10327c6d0.leading   (active)>",
"<NSLayoutConstraint:0x280d72300 'UIView-Encapsulated-Layout-Width' UIStackView:0x10327c250.width == 0   (active)>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x280d5f610 UIImageView:0x10327c6d0.width == 60   (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2019-10-12 10:43:30.598068-0500 Fourmi[1708:291157] [LayoutConstraints] 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. 
(
"<NSLayoutConstraint:0x280d5f5c0 UIImageView:0x10327c6d0.height == 60   (active)>",
"<NSLayoutConstraint:0x280d72b70 'UISV-canvas-connection' UIStackView:0x10327c250.top == UIImageView:0x10327c6d0.top   (active)>",
"<NSLayoutConstraint:0x280d72bc0 'UISV-canvas-connection' V:[UILabel:0x10327e990'Steve Rodriquez']-(0)-|   (active, names: '|':UIStackView:0x10327c250 )>",
"<NSLayoutConstraint:0x280d72c60 'UISV-spacing' V:[UIImageView:0x10327c6d0]-(5)-[UILabel:0x10327e990'Steve Rodriquez']   (active)>",
"<NSLayoutConstraint:0x280d72580 'UIView-Encapsulated-Layout-Height' UIStackView:0x10327c250.height == 0   (active)>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x280d5f5c0 UIImageView:0x10327c6d0.height == 60   (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2019-10-12 10:43:30.598469-0500 Fourmi[1708:291157] [LayoutConstraints] 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. 
(
"<NSLayoutConstraint:0x280d72b70 'UISV-canvas-connection' UIStackView:0x10327c250.top == UIImageView:0x10327c6d0.top   (active)>",
"<NSLayoutConstraint:0x280d72bc0 'UISV-canvas-connection' V:[UILabel:0x10327e990'Steve Rodriquez']-(0)-|   (active, names: '|':UIStackView:0x10327c250 )>",
"<NSLayoutConstraint:0x280d72c60 'UISV-spacing' V:[UIImageView:0x10327c6d0]-(5)-[UILabel:0x10327e990'Steve Rodriquez']   (active)>",
"<NSLayoutConstraint:0x280d72580 'UIView-Encapsulated-Layout-Height' UIStackView:0x10327c250.height == 0   (active)>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x280d72c60 'UISV-spacing' V:[UIImageView:0x10327c6d0]-(5)-[UILabel:0x10327e990'Steve Rodriquez']   (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2019-10-12 10:43:30.598894-0500 Fourmi[1708:291157] [LayoutConstraints] 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:0x280d40000 h=--& v=--& UIStackView:0x10327c250.width == 0   (active)>",
"<NSLayoutConstraint:0x280d5ff20 UIStackView:0x10327c250.width == UITableViewCellContentView:0x10327fdb0.width   (active)>",
"<NSLayoutConstraint:0x280d40370 'fittingSizeHTarget' UITableViewCellContentView:0x10327fdb0.width == 375   (active)>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x280d5ff20 UIStackView:0x10327c250.width == UITableViewCellContentView:0x10327fdb0.width   (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2019-10-12 10:43:30.599762-0500 Fourmi[1708:291157] [Warning] Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a table view cell's content view. We're considering the collapse unintentional and using standard height instead. Cell: <Fourmi.ChatTableViewCell: 0x10327af70; baseClass = UITableViewCell; frame = (0 0; 375 44); autoresize = W; layer = <CALayer: 0x282e41300>>

添加userView_stackView.translatesAutoresizingMaskIntoConstraints=false修复它

最新更新