简体   繁体   English

使用具有固定宽度和高度的 imageview 在内部添加两个视图时,stackview 约束中断

[英]stackview constraints break when adding two views inside with imageview having fixed width and height

i have a stackview with an imageview and a label underneath it but when i try to add it into stackview it doesnt appear and apparently all the constraints break.i want the imageview to be fixed width and height and the stackview to equal the width of the label我有一个带有 imageview 和 label 的堆栈视图,但是当我尝试将其添加到堆栈视图中时,它没有出现并且显然所有约束都中断了。我希望 Z6C666B7F9E6BE3A4F92F92F71FF55F6848 的宽度固定为宽度label

    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
}

Adding views like this breaks the constraints and prints the following error on the console where all the constraints are breaking添加这样的视图会破坏约束并在所有约束都被破坏的控制台上打印以下错误

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

adding userView_stackView.translatesAutoresizingMaskIntoConstraints=false fixed it添加userView_stackView.translatesAutoresizingMaskIntoConstraints=false修复它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 水平 StackView 内的 ImageView 不考虑约束高度 - ImageView inside horizontal StackView is not respecting constraint height 设置为使ImageView自动将图像大小调整为固定宽度但动态高度的约束是什么? - What constraints to set to have an ImageView automatically resize image to a fixed width but dynamic height? 快速约束打破了框架的宽度和高度 - Swift constraints break frame width and height 无约束时如何设置imageview的高度 - How to set height of imageview when no constraints iOS:更改StackView(在StackView内部)中TextField的高度 - iOS: Change height of TextField in StackView (inside StackView) Stackview里面的其他stackview宽度问题 - Stackview inside Other stackview width issue Swift - StackView 和添加约束,以编程方式添加视图到堆栈 - 加载后调整单元格高度? - Swift - StackView and Adding Constraints With Programmatically Added Views To Stack - Adjust Cell Hight After Load? 更新imageView swift 4的高度约束时,无法同时满足约束 - Unable to simultaneously satisfy constraints when update height constraints of imageView swift 4 隐藏 stackView 时打破约束 - Breaking constraints when the stackView is hidden 在UIStackView中为视图添加大小约束 - Adding Size Constraints for Views inside a UIStackView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM