Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS expand UITableViewCell on click with AutoLayout

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:

enter image description here

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 )>
like image 968
MichalMoskala Avatar asked Nov 23 '22 01:11

MichalMoskala


1 Answers

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.

like image 105
rushisangani Avatar answered Dec 11 '22 20:12

rushisangani