NSLayoutConstraint animation issue on iOS 10-11

2019-08-18 09:08发布

I'm trying to implement show/hide animation for a view. The idea is to resize view's height constraint and to let its superview to resize with it. To achieve this I add view's height constraint and also pin its bottom constraint to superview's bottom (so I do not need specifying superview's height constraint)

enter image description here

On iOS 9 it works as expected:

enter image description here

And this happens on iOS 10-11:

enter image description here

Animation code:

#import "ViewController.h"

@interface ViewController ()
{
    BOOL _hideFlag;
    CGFloat _redViewHeight;
}

@property (strong, nonatomic) IBOutlet UIView *containerView;
@property (strong, nonatomic) IBOutlet UIButton *toggleButton;
@property (strong, nonatomic) IBOutlet UIView *redView;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *redViewHeightConstraint;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [_toggleButton addTarget:self action:@selector(toggle:) forControlEvents:UIControlEventTouchUpInside];
    _redViewHeight = _redViewHeightConstraint.constant;
}

- (void)toggle:(UIButton *)sender
{
    _hideFlag = !_hideFlag;
    [_containerView layoutIfNeeded];

    [UIView animateWithDuration:0.2 animations:^{
        _redViewHeightConstraint.constant = _hideFlag ? 0 : _redViewHeight;
        [_containerView layoutIfNeeded];
    }];
}

@end

Edit

Solved thanks to @Kuldeep. Just to emphasize: the point is to call layoutIfNeeded at least on the upper affected view's superview in the hierarchy. So in my case since the height of the containerView is also changing I had to call layoutIfNeeded on containerView 's superview.

1条回答
甜甜的少女心
2楼-- · 2019-08-18 09:39

Try this it works in iOS 9,10,11

Objective C

- (IBAction)btnChangeTapped:(UIButton *)sender {
    sender.selected =! sender.selected;

    if (sender.selected) {
        [self.view layoutIfNeeded];
        [UIView animateWithDuration:1.0 animations:^{
            self.constraintHeightOfView.constant = 100.0; // as per your require
            [self.view layoutIfNeeded];
        }];
    }
    else {
        [self.view layoutIfNeeded];
        [UIView animateWithDuration:1.0 animations:^{
            self.constraintHeightOfView.constant = 350.0; // Back to Normal
            [self.view layoutIfNeeded];
        }];
    }
}

Swift 5.0

@IBAction func btnChangeTapped(_ sender: UIButton) {
    sender.isSelected = !sender.isSelected

    if sender.isSelected {
        self.view.layoutIfNeeded()
        UIView.animate(withDuration: 1.0, animations: {
            self.constraintHeightOfView.constant = 100.0 // as per your require
            self.view.layoutIfNeeded()
        })
    } else {
        self.view.layoutIfNeeded()
        UIView.animate(withDuration: 1.0, animations: {
            self.constraintHeightOfView.constant = 350.0 // Back to Normal
            self.view.layoutIfNeeded()
        })
    }
}
查看更多
登录 后发表回答