3
- (IBAction)btnA:(id)sender {
    if(self.height.constant == 300) {
         self.height.constant = 50;
    } else {
         self.height.constant = 300;
    }

    [self.subView1 setNeedsUpdateConstraints];

    [UIView animateWithDuration:1.0 animations:^{
        [self.subView1 layoutIfNeeded];
    }];
}

I am using this code but on iOS10 its not animating its just jump increasing and decreasing mySubview1 height. Why?

Same code is working fine in iOS9

S.J
  • 3,063
  • 3
  • 33
  • 66
  • Could you try removing this line and see if it works. [self.subView1 setNeedsUpdateConstraints]; – Xchord Sep 27 '16 at 10:27

1 Answers1

9

For iOS10 its the view's superview which you have to force layout.

//Force apply all constraints prior to the change in constant.
[self.view.superview layoutIfNeeded];

//Update constant
self.height.constant = 0;

[UIView animateWithDuration:1.0 animations:^{
    [self.view.superview layoutIfNeeded];
} 

I believe this behaviour is caused by the change in Xcode8 - iOS10 layout

References:

https://stackoverflow.com/a/39501882

https://stackoverflow.com/a/39548367/1045672

Community
  • 1
  • 1
Teffi
  • 2,498
  • 4
  • 21
  • 40
  • This issue used to exist in iOS 7 too. Check this out http://stackoverflow.com/questions/23028701/auto-layout-uiview-on-constraint-change-does-not-animate/23102912#23102912 – BangOperator Oct 01 '16 at 09:01
  • If my view is subview of UITableViewCell then what should I do? Do I use 1. [cell.subView.superView layoutIfNeeded], 2.[cell layoutIfNeeded] or 3. [cell.superview layoutIfNeed]? Which would I use? I'm using the same code but not working in iOS 10.1. Please help me. Thanks – Jekil Patel Nov 09 '16 at 13:18
  • @JekilPatel what have you tried? Am I correct that you want to animate a subview inside a uitableviewcell? try [cell.contentview layoutifneeded] – Teffi Nov 09 '16 at 14:28
  • @Teffi Yes, you are right. I'm trying to animate subView inside UITableViewCell. Thanks for the reply. Let me try with the one you provide and let you know. Thanks a lot – Jekil Patel Nov 10 '16 at 04:24
  • @Teffi I was trying to animate tableview as a subview inside the UITableViewCell with [cell.contentview layoutifneeded] unfortunately it's not working. I'm doing same code as you answered. – Jekil Patel Nov 10 '16 at 04:49