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)
On iOS 9 it works as expected:
And this happens on iOS 10-11:
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.
Try this it works in iOS 9,10,11
Objective C
Swift 5.0