Skip to content

Commit 30b2942

Browse files
committed
Fixed a height constraint issue on iPad sheets
1 parent b14abc2 commit 30b2942

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Sources/KeyboardLayoutGuide/KeyboardLayoutGuide.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,22 @@ open class KeyboardLayoutGuide: UILayoutGuide {
112112
if #available(iOS 11.0, *), usesSafeArea, height > 0, let bottom = owningView?.safeAreaInsets.bottom {
113113
height -= bottom
114114
}
115+
116+
// Factor in the non full screen views (iPad sheets)
117+
if let activeWindow = UIApplication.shared.activeWindow, let owningView = owningView, height > 0 {
118+
// Owning view's frame in the active window
119+
let owningFrameInRoot = owningView.convert(owningView.frame, to: activeWindow)
120+
// We have to ignore the part of the owning view that's outside of the active window
121+
// (Modal presentation)
122+
let intersectionFrame = activeWindow.frame.intersection(owningFrameInRoot)
123+
124+
let windowHeight = activeWindow.frame.height
125+
let bottomDifference = windowHeight - (intersectionFrame.height + intersectionFrame.origin.y)
126+
127+
height -= bottomDifference
128+
}
129+
130+
115131
heightConstraint?.constant = height
116132
if duration > 0.0 {
117133
animate(note)
@@ -178,3 +194,23 @@ func isVisible(view: UIView) -> Bool {
178194
}
179195
return isVisible(view: view, inView: view.superview)
180196
}
197+
198+
extension UIApplication {
199+
200+
// Finds the currently active window, This works similar to the
201+
// deprecated `keyWindow` however it supports multi-window'd
202+
// iPad apps
203+
var activeWindow: UIWindow? {
204+
if #available(iOS 13, *) {
205+
return connectedScenes
206+
.filter { $0.activationState == .foregroundActive }
207+
.map { $0 as? UIWindowScene }
208+
.compactMap { $0 }
209+
.first?.windows
210+
.first { $0.isKeyWindow }
211+
} else {
212+
return keyWindow
213+
}
214+
}
215+
216+
}

0 commit comments

Comments
 (0)