ios 在imageview内部添加两个具有固定宽度和高度的视图时stackview约束断开[duplicate]

bybem2ql  于 2023-02-20  发布在  iOS
关注(0)|答案(1)|浏览(111)
    • 此问题在此处已有答案**:

Strange UIView-Encapsulated-Layout-Height Error(11个答案)
十小时前关门了。
我有一个stackview与一个imageview和一个标签在它下面但是当我尝试到添加它到stackview它不显现并且显然所有的约束break.我想要imageview是固定的宽度和高度并且stackview到相等这宽度的标签

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

yhived7q1#

添加userView_stackView.translatesAutoresizingMaskIntoConstraints=false修复了它

相关问题