I'm working on simple application and now I'm focused on expanding UITableViewCell after user tap that cell. It's iOS 8 app so I have set:
self.tableVIew.rowHeight = UITableViewAutomaticDimension
self.tableVIew.estimatedRowHeight = 50
cell constraints look like this:
If user tap cell this function is called:
func extend() {
self.contentView.removeConstraint(self.bottomConstraint)
let additionalView = UIView()
additionalView.setTranslatesAutoresizingMaskIntoConstraints(false)
additionalView.backgroundColor = UIColor.orangeColor()
self.contentView.addSubview(additionalView)
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[additionalView(50)]-5-|", options: nil, metrics: nil, views: ["additionalView" : additionalView]))
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-5-[additionalView(100)]-5@999-|", options: nil, metrics: nil, views: ["additionalView" : additionalView]))
}
self.bottomConstraint is costraint between green circle bottom and cell contentView bottom.
The question is: Why this solution works only if there is priority set in constraint:
V:|-5-[additionalView(100)]-5@999-|
?
Without explicit priority I got errors:
(
"<NSLayoutConstraint:0x7a63f7a0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7a737840(60)]>",
"<NSLayoutConstraint:0x7a644f40 V:|-(5)-[UIView:0x7a63f2a0] (Names: '|':UITableViewCellContentView:0x7a737840 )>",
"<NSLayoutConstraint:0x7a644fb0 V:[UIView:0x7a63f2a0(100)]>",
"<NSLayoutConstraint:0x7a644f70 V:[UIView:0x7a63f2a0]-(5)-| (Names: '|':UITableViewCellContentView:0x7a737840 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7a644f70 V:[UIView:0x7a63f2a0]-(5)-| (Names: '|':UITableViewCellContentView:0x7a737840 )>
I have made a demo that gives you the exact behaviour as you want. here is the link: https://github.com/rushisangani/TableViewCellExpand
Please set constraint of your expand/ collapse views as described in demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With