简体   繁体   中英

UITableViewCell - unable to simultaneously satisfy constraints

I'm using SnapKit in order to make constraints and am creating views programatically. However, there's one thing I can't grasp.

I want to add a child UIView into the content view of a table view cell and set its height. This already causes a unsatisfiable constraint error.

I've tried also setting (estimated)rowHeight in the table view, but that did not help.

2018-07-14 19:21:38.785556+0200[20027:748731] [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. 
(
    "<SnapKit.LayoutConstraint:0x6000000b6500@WeatherCell.swift#24 UIView:0x7fa342409240.top == UITableViewCellContentView:0x7fa342413970.top>",
    "<SnapKit.LayoutConstraint:0x6000000b7880@WeatherCell.swift#24 UIView:0x7fa342409240.bottom == UITableViewCellContentView:0x7fa342413970.bottom>",
    "<SnapKit.LayoutConstraint:0x6000000b8a80@WeatherCell.swift#26 UIView:0x7fa342409240.height == 145.784842967987>",
    "<NSLayoutConstraint:0x604000282210 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fa342413970.height == 146   (active)>"
)

Will attempt to recover by breaking constraint 
<SnapKit.LayoutConstraint:0x6000000b8a80@WeatherCell.swift#26 UIView:0x7fa342409240.height == 145.784842967987>

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.

Cell code:

class WeatherCell: UITableViewCell {
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        let expandView = UIView()

        self.contentView.addSubview(expandView)

        expandView.snp.makeConstraints { view in
            view.edges.equalToSuperview()

            view.height.equalTo(CGFloat(Float(arc4random()) / Float(UINT32_MAX)) * 200)
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

You need to reference the bottom constraint and change it's priority to 999 / high but not to required , so remove the edges constraint and make them separately

let expandView = UIView()
self.contentView.addSubview(expandView)
expandView.snp.makeConstraints { (view) in
    view.bottom.equalTo(self.contentView).priority(999) // can also be .priority(.high)
    view.top.left.right.equalTo(self.contentView)
    view.height.equalTo(CGFloat(Float(arc4random()) / Float(UINT32_MAX)) * 200)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM