Animating a subview with CentreY constraint

41 views Asked by At

I have a UITableViewCell with two UILabels and a UISwitch. The labels are multilined and placed on top of each other. The UISwitch is centred vertically. When I turn off the switch I hide one of the labels and the height of the row changes. This causes the switch to jump though I call:

[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

Here is my tableviewcell class implementation:

 - (void)setBounds:(CGRect)bounds
{
  [super setBounds:bounds];

  self.contentView.frame = self.bounds;
}

- (void)setUpView{

    self.numberLabel.text = [NSString stringWithFormat:@"Quote number %ld",    [self.cellData[@"rowNumber"] integerValue]];
    [self.controlSwitch setOn:[self.cellData[@"checked"] boolValue]];

    [UIView animateWithDuration:0.5 animations:^{
    if ([self.controlSwitch isOn]) {
        self.quoteLabel.text = self.cellData[@"quote"];
        self.quoteLabel.hidden = NO;
        self.bottomConst.constant = 10;
    }else{
        self.quoteLabel.text = @"";
        self.quoteLabel.hidden = YES;
        self.bottomConst.constant = 0;
    }
 }];


}

- (void)layoutSubviews
{
  [super layoutSubviews];

  self.selectionStyle = UITableViewCellSelectionStyleNone;
  [self.contentView updateConstraintsIfNeeded];
  [self.contentView layoutIfNeeded];

  self.numberLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.numberLabel.frame);
  self.quoteLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.quoteLabel.frame);
}


- (IBAction)removeQuote:(id)sender {
   [self.delegate updateCell:self];
}

The constraint settings on the UITableViewCell looks like this.

enter image description here

When the switch is turned on, I'm simply unhide the Quote label and set the text. Here is the link on how it looks like. Is this the default behavior or is there a way I can control the movement of the switch when the row animates?

1

There are 1 answers

1
Bannings On

Try this:

...
[self.contentView updateConstraintsIfNeeded];

[UIView beginAnimations:nil context:nil];
[self.contentView layoutIfNeeded];
[UIView commitAnimations];
...