diff --git a/ios/extensions/UIView.swift b/ios/extensions/UIView.swift index f3370c28b3..d0e11243c3 100644 --- a/ios/extensions/UIView.swift +++ b/ios/extensions/UIView.swift @@ -14,6 +14,26 @@ public extension UIView { let rootView = UIApplication.shared.activeWindow?.rootViewController?.view return superview?.convert(frame, to: rootView) } + + func isVisibleInHierarchy(initial: Bool = true) -> Bool { + guard let window = window else { + return false + } + if isHidden || alpha == 0.0 { + return false + } + if superview === window { + return true + } else if let superview = superview { + if initial, frame.minY >= superview.frame.height { + return false + } else { + return superview.isVisibleInHierarchy(initial: false) + } + } else { + return false + } + } } public extension Optional where Wrapped == UIView { @@ -32,4 +52,11 @@ public extension Optional where Wrapped == UIView { return (position, frameY) } + + func isVisibleInHierarchy(initial: Bool = true) -> Bool { + guard let view = self else { + return false + } + return view.isVisibleInHierarchy(initial: initial) + } } diff --git a/ios/extensions/UIWindow.swift b/ios/extensions/UIWindow.swift index c73fc1be4a..0726577335 100644 --- a/ios/extensions/UIWindow.swift +++ b/ios/extensions/UIWindow.swift @@ -37,8 +37,11 @@ public extension UIWindow { } func getTopWindow() -> UIWindow? { - // Return the keyboard window if it's available, otherwise return the last window - return keyboardWindow ?? UIApplication.shared.activeWindow + let keyboardView = KeyboardView.find() + // return the keyboard window if it's available and keyboard is visible, otherwise return the last window + return (keyboardWindow != nil && keyboardView.isVisibleInHierarchy()) + ? keyboardWindow + : UIApplication.shared.activeWindow } }