From feb5a0c444e8e8b1d7f52f7ff068dbc4fd2f470b Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Fri, 23 Nov 2018 17:42:03 -0600 Subject: [PATCH] fix initial CaptionView layout glitch --- .../AttachmentApprovalViewController.swift | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/SignalMessaging/ViewControllers/AttachmentApprovalViewController.swift b/SignalMessaging/ViewControllers/AttachmentApprovalViewController.swift index cff9ab1bb..d3dd9014d 100644 --- a/SignalMessaging/ViewControllers/AttachmentApprovalViewController.swift +++ b/SignalMessaging/ViewControllers/AttachmentApprovalViewController.swift @@ -681,7 +681,7 @@ public class AttachmentPrepViewController: OWSViewController, PlayerProgressBarD fatalError("init(coder:) has not been implemented") } - // MARK: - View Lifecycle + // MARK: - Subviews lazy var captionView: CaptionView = { return CaptionView(attachmentItem: attachmentItem) @@ -695,6 +695,8 @@ public class AttachmentPrepViewController: OWSViewController, PlayerProgressBarD return touchInterceptorView }() + // MARK: - View Lifecycle + override public func loadView() { self.view = UIView() @@ -821,6 +823,7 @@ public class AttachmentPrepViewController: OWSViewController, PlayerProgressBarD // MARK: CaptionView lifts with keyboard + var hasLaidOutCaptionView: Bool = false var captionViewBottomConstraint: NSLayoutConstraint! func updateCaptionViewBottomInset() { guard let prepDelegate = self.prepDelegate else { @@ -828,8 +831,21 @@ public class AttachmentPrepViewController: OWSViewController, PlayerProgressBarD return } - captionViewBottomConstraint.constant = -prepDelegate.desiredCaptionViewBottomInset - captionView.superview?.layoutIfNeeded() + let changeBlock = { + self.captionViewBottomConstraint.constant = -prepDelegate.desiredCaptionViewBottomInset + self.captionView.superview?.layoutIfNeeded() + } + + // To avoid an animation glitch, we apply this update without animation before initial + // appearance. But after that, we want to apply the constraint change within the existing + // animation context, since we call this while handling a UIKeyboard notification, which + // allows us to slide up the CaptionView in lockstep with the keyboard. + if hasLaidOutCaptionView { + changeBlock() + } else { + hasLaidOutCaptionView = true + UIView.performWithoutAnimation { changeBlock() } + } } // MARK: - Event Handlers