Skip to content

Commit af93672

Browse files
authored
Merge pull request #39 from a2i2/fix/dismissal-height
Solve a rare problem where keyboard height will be full-screen on dismissal
2 parents d3ebfa7 + 63f7652 commit af93672

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

Sources/KeyboardLayoutGuide/KeyboardLayoutGuide.swift

+18-6
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,17 @@ open class KeyboardLayoutGuide: UILayoutGuide {
6363
// Observe keyboardWillChangeFrame notifications
6464
notificationCenter.addObserver(
6565
self,
66-
selector: #selector(keyboardWillChangeFrame(_:)),
66+
selector: #selector(adjustKeyboard(_:)),
6767
name: UIResponder.keyboardWillChangeFrameNotification,
6868
object: nil
6969
)
70+
// Observe keyboardWillHide notifications
71+
notificationCenter.addObserver(
72+
self,
73+
selector: #selector(adjustKeyboard(_:)),
74+
name: UIResponder.keyboardWillHideNotification,
75+
object: nil
76+
)
7077
}
7178

7279
internal func setUp() {
@@ -100,7 +107,7 @@ open class KeyboardLayoutGuide: UILayoutGuide {
100107
}
101108

102109
@objc
103-
private func keyboardWillChangeFrame(_ note: Notification) {
110+
private func adjustKeyboard(_ note: Notification) {
104111
if var height = note.keyboardHeight, let duration = note.animationDuration {
105112
if #available(iOS 11.0, *), usesSafeArea, height > 0, let bottom = owningView?.safeAreaInsets.bottom {
106113
height -= bottom
@@ -142,10 +149,15 @@ extension Notification {
142149
guard let keyboardFrame = userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {
143150
return nil
144151
}
145-
// Weirdly enough UIKeyboardFrameEndUserInfoKey doesn't have the same behaviour
146-
// in ios 10 or iOS 11 so we can't rely on v.cgRectValue.width
147-
let screenHeight = UIApplication.shared.keyWindow?.bounds.height ?? UIScreen.main.bounds.height
148-
return screenHeight - keyboardFrame.cgRectValue.minY
152+
153+
if name == UIResponder.keyboardWillHideNotification {
154+
return 0.0
155+
} else {
156+
// Weirdly enough UIKeyboardFrameEndUserInfoKey doesn't have the same behaviour
157+
// in ios 10 or iOS 11 so we can't rely on v.cgRectValue.width
158+
let screenHeight = UIApplication.shared.keyWindow?.bounds.height ?? UIScreen.main.bounds.height
159+
return screenHeight - keyboardFrame.cgRectValue.minY
160+
}
149161
}
150162

151163
var animationDuration: CGFloat? {

0 commit comments

Comments
 (0)