From 97603e64cc8966f9468d3c95eb11567651610dfd Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 5 Mar 2019 10:06:39 -0800 Subject: [PATCH] Deconflict "bottom view" layout and keyboard animations. --- .../ViewControllers/OWSViewController.m | 41 ++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/SignalMessaging/ViewControllers/OWSViewController.m b/SignalMessaging/ViewControllers/OWSViewController.m index b4d8a06ec..4019173e7 100644 --- a/SignalMessaging/ViewControllers/OWSViewController.m +++ b/SignalMessaging/ViewControllers/OWSViewController.m @@ -23,6 +23,7 @@ UIInterfaceOrientationMask DefaultUIInterfaceOrientationMask(void) @property (nonatomic, weak) UIView *bottomLayoutView; @property (nonatomic) NSLayoutConstraint *bottomLayoutConstraint; +@property (nonatomic) BOOL shouldAnimatedBottomLayout; @end @@ -64,6 +65,22 @@ UIInterfaceOrientationMask DefaultUIInterfaceOrientationMask(void) return self; } +#pragma mark - View Lifecycle + +- (void)viewDidAppear:(BOOL)animated +{ + [super viewDidAppear:animated]; + + self.shouldAnimatedBottomLayout = YES; +} + +- (void)viewDidDisappear:(BOOL)animated +{ + [super viewDidDisappear:animated]; + + self.shouldAnimatedBottomLayout = NO; +} + - (void)viewDidLoad { [super viewDidLoad]; @@ -73,6 +90,8 @@ UIInterfaceOrientationMask DefaultUIInterfaceOrientationMask(void) } } +#pragma mark - + - (void)autoPinViewToBottomOfViewControllerOrKeyboard:(UIView *)view avoidNotch:(BOOL)avoidNotch { OWSAssertDebug(view); @@ -185,11 +204,23 @@ UIInterfaceOrientationMask DefaultUIInterfaceOrientationMask(void) // bar. CGFloat offset = -MAX(0, (self.view.height - self.bottomLayoutGuide.length - keyboardEndFrameConverted.origin.y)); - // There's no need to use: [UIView animateWithDuration:...]. - // Any layout changes made during these notifications are - // automatically animated. - self.bottomLayoutConstraint.constant = offset; - [self.bottomLayoutView.superview layoutIfNeeded]; + // UIKit by default animates all changes in response to keyboard events. + // We want to suppress those animations if the view isn't visible, + // otherwise presentation animations don't work properly. + dispatch_block_t updateLayout = ^{ + // There's no need to use: [UIView animateWithDuration:...]. + // Any layout changes made during these notifications are + // automatically animated. + self.bottomLayoutConstraint.constant = offset; + [self.bottomLayoutView.superview layoutIfNeeded]; + }; + + + if (self.shouldAnimatedBottomLayout) { + updateLayout(); + } else { + [UIView performWithoutAnimation:updateLayout]; + } } #pragma mark - Orientation