Reordering Custom Cell Causes the Constraints to get Messed Up

删除回忆录丶 提交于 2019-12-25 02:35:13

问题


I have a custom cell which has a reordering problem that looks like this:

Whenever I try to change the position of the cell, a weird thing happens, where the constraints break and half of the cell goes off screen then, when I exit editing mode, the cell diminishes into a small rectangle in the middle of the place where the cell used to be.

This is the console message which comes when I try to reorder a cell.

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:0x283a7cbe0 'Content View Leading to Shadow View Leading' H:|-(0)-[FriendZone.ShadowView:0x10512d210]   (active, names: '|':UITableViewCellContentView:0x105139d30 )>",
    "<NSLayoutConstraint:0x283a7cb90 'Content View Trailing to Shadow View Trailing' H:[FriendZone.ShadowView:0x10512d210]-(0)-|   (active, names: '|':UITableViewCellContentView:0x105139d30 )>",
    "<NSLayoutConstraint:0x283a7cb40 'Main Background Leading' H:|-(0)-[UIView:0x105139fb0]   (active, names: '|':FriendZone.ShadowView:0x10512d210 )>",
    "<NSLayoutConstraint:0x283a7caa0 'Main Background Trailing' H:[UIView:0x105139fb0]-(0)-|   (active, names: '|':FriendZone.ShadowView:0x10512d210 )>",
    "<NSLayoutConstraint:0x283a7c8c0 'Stack View Center X' UIStackView:0x10513a190.centerX == UIView:0x105139fb0.centerX   (active)>",
    "<NSLayoutConstraint:0x283a7c910 'Stack View Trailing' H:[UIStackView:0x10513a190]-(15)-|   (active, names: '|':UIView:0x105139fb0 )>",
    "<NSLayoutConstraint:0x283a7c780 'Time Width' UILabel:0x10513a880'9:48PM'.width <= 0.4*UIStackView:0x10513a190.width   (active)>",
    "<NSLayoutConstraint:0x283a79220 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x105139d30.width == 26.5   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x283a7c780 'Time Width' UILabel:0x10513a880'9:48PM'.width <= 0.4*UIStackView:0x10513a190.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.

I tried searching for answers and debugging my constraints, but I still couldn't come up with a fix, let alone understand what's going on.

Here is the view hierarchy for the cell, along with its constraints.

And this is the custom class for the cell.

class PersonCell: UITableViewCell {
    @IBOutlet var name: UILabel!
    @IBOutlet var time: UILabel!
    @IBOutlet var mainBackground: UIView!
    @IBOutlet var shadowLayer: UIView!

    let gradientLayer = CAGradientLayer()

    override func layoutSubviews() {
        super.layoutSubviews()
        gradientLayer.frame = self.bounds
    }

    // To add left and right margins to tableViewCell and adjust gradientLayer's frame before the gradient is set
    override var frame: CGRect {
        get {
            return super.frame
        }
        set {
            var frame = newValue
            frame.origin.x += 5
            frame.size.width -= 2 * 5

            super.frame = frame
        }
    }
}

来源:https://stackoverflow.com/questions/57777292/reordering-custom-cell-causes-the-constraints-to-get-messed-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!