user2159978
user2159978

Reputation: 2709

Totally change constraint in iOS?

In my case I have a cell 2 labels (one under another one) and an icon to the left of them. These elements and their constraints (including the goal constraint) are mentioned in xib file.

If the first label is visible then the constraint should be "vertical spacing" between these 2 labels with value 0.

If the first label is hidden then the constraint should be "center y" to align label to the icon's center.

I know how to do it separately but how to switch between them? An additional difficulty is that all these elements are inside the cell so I should reuse them instead of creating-deleting.

EDITED

Two various situations I want to achieve in one cell:

enter image description here

UPDATED

I tried to use the following code:

[cell removeConstraint:cell.cstrSendingFileStatus];
                    cell.cstrSendingFileStatus = [NSLayoutConstraint constraintWithItem:cell.label2
                                                                              attribute:NSLayoutAttributeCenterY
                                                                              relatedBy:NSLayoutRelationEqual
                                                                                 toItem:cell.imgView
                                                                              attribute:NSLayoutAttributeCenterY
                                                                             multiplier:1
                                                                               constant:0];
                    [cell addConstraint:cell.cstrSendingFileStatus];

The first cell is even drawn correctly but I have troubles with next cells: there a lot of messages in console that the app tries to break imageView height constraint.

Upvotes: 0

Views: 124

Answers (1)

Aris
Aris

Reputation: 1559

A simple solution is to have a height constraint in the first label. Instead of hiding it, you can set the height constant to 0. That way the second Label will expand all the way to the top where the first label would be.

Edit:

To make this work you should put a Bottom Space to Superview = 0 or something in the second label.That way the second label is pinned to the bottom and it wont move, it will expand based on the first labels height. Also the second label should not have a height constraint, only the first.

Edit #2: The second label should have left right top(to first label) bottom(to superview), no height

Upvotes: 0

Related Questions