diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2021-04-29 16:37:25 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2021-04-29 16:37:25 +0200 |
| commit | 21a4e7e1797c394cb4506cf8c2aa2ac5a770ce7e (patch) | |
| tree | 4fc16a2d5cf325cb70ae98b817880bc525cf09e2 | |
| parent | 3f9204e4af119b85c8edcce709b05ca09d32973f (diff) | |
| parent | b9d446b6a53f6f6ddfaa5558d27c706b9857d3b0 (diff) | |
| download | mullvadvpn-21a4e7e1797c394cb4506cf8c2aa2ac5a770ce7e.tar.xz mullvadvpn-21a4e7e1797c394cb4506cf8c2aa2ac5a770ce7e.zip | |
Merge branch 'keyboard-in-formsheet'
| -rw-r--r-- | ios/MullvadVPN/AutomaticKeyboardResponder.swift | 33 | ||||
| -rw-r--r-- | ios/MullvadVPN/LoginViewController.swift | 5 | ||||
| -rw-r--r-- | ios/MullvadVPN/ProblemReportViewController.swift | 5 |
3 files changed, 35 insertions, 8 deletions
diff --git a/ios/MullvadVPN/AutomaticKeyboardResponder.swift b/ios/MullvadVPN/AutomaticKeyboardResponder.swift index f427b71bd0..04518a40ef 100644 --- a/ios/MullvadVPN/AutomaticKeyboardResponder.swift +++ b/ios/MullvadVPN/AutomaticKeyboardResponder.swift @@ -7,6 +7,7 @@ // import UIKit +import Logging class AutomaticKeyboardResponder { weak var targetView: UIView? @@ -15,6 +16,7 @@ class AutomaticKeyboardResponder { private var showsKeyboard = false private var lastKeyboardRect: CGRect? + private let logger = Logger(label: "AutomaticKeyboardResponder") private var presentationFrameObserver: NSKeyValueObservation? init<T: UIView>(targetView: T, handler: @escaping (T, CGFloat) -> Void) { @@ -65,16 +67,17 @@ class AutomaticKeyboardResponder { } private func addPresentationControllerObserver() { + guard isFormSheetPresentation else { return } + // Presentation controller follows the keyboard on iPad. // Install the observer to listen for the container view frame and adjust the target view // accordingly. - guard let containerView = parentViewController?.presentationController?.containerView, isFormSheetPresentation else { return } - - let containingView = containerView.subviews.first { (subview) -> Bool in - return targetView?.isDescendant(of: subview) ?? false + guard let containerView = presentationContainerView else { + logger.warning("Cannot determine the container view in form sheet presentation.") + return } - presentationFrameObserver = containingView?.observe(\.frame, options: [.new], changeHandler: { [weak self] (containingView, change) in + presentationFrameObserver = containerView.observe(\.frame, options: [.new], changeHandler: { [weak self] (containingView, change) in guard let self = self, let keyboardFrameValue = self.lastKeyboardRect else { return } self.adjustContentInsets(keyboardRect: keyboardFrameValue) @@ -85,14 +88,28 @@ class AutomaticKeyboardResponder { private var parentViewController: UIViewController? { var responder: UIResponder? = targetView let iterator = AnyIterator { () -> UIResponder? in - let next = responder?.next - responder = next - return next + responder = responder?.next + return responder } return iterator.first { $0 is UIViewController } as? UIViewController } + /// Returns the presentation container view that's moved along with the keyboard on iPad + private var presentationContainerView: UIView? { + var currentView = parentViewController?.view + let iterator = AnyIterator { () -> UIView? in + currentView = currentView?.superview + return currentView + } + + // Find the container view that private `_UIFormSheetPresentationController` moves + // along with the keyboard. + return iterator.first { (view) -> Bool in + return view.description.starts(with: "<UIDropShadowView") + } + } + private var isFormSheetPresentation: Bool { return UIDevice.current.userInterfaceIdiom == .pad && parentViewController?.modalPresentationStyle == .formSheet diff --git a/ios/MullvadVPN/LoginViewController.swift b/ios/MullvadVPN/LoginViewController.swift index c08b7f4e82..ae0d1d4e05 100644 --- a/ios/MullvadVPN/LoginViewController.swift +++ b/ios/MullvadVPN/LoginViewController.swift @@ -118,6 +118,11 @@ class LoginViewController: UIViewController, RootContainment { object: contentView.accountTextField) } + override var disablesAutomaticKeyboardDismissal: Bool { + // Allow dismissing the keyboard in .formSheet presentation style + return false + } + // MARK: - Public func reset() { diff --git a/ios/MullvadVPN/ProblemReportViewController.swift b/ios/MullvadVPN/ProblemReportViewController.swift index 6f134a049c..0832fd920c 100644 --- a/ios/MullvadVPN/ProblemReportViewController.swift +++ b/ios/MullvadVPN/ProblemReportViewController.swift @@ -165,6 +165,11 @@ class ProblemReportViewController: UIViewController, UITextFieldDelegate, Condit // MARK: - View lifecycle + override var disablesAutomaticKeyboardDismissal: Bool { + // Allow dismissing the keyboard in .formSheet presentation style + return false + } + override func viewDidLoad() { super.viewDidLoad() |
