Skip to content

Commit

Permalink
Merge pull request #39 from a2i2/fix/dismissal-height
Browse files Browse the repository at this point in the history
Solve a rare problem where keyboard height will be full-screen on dismissal
  • Loading branch information
s4cha authored Jan 17, 2022
2 parents d3ebfa7 + 63f7652 commit af93672
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions Sources/KeyboardLayoutGuide/KeyboardLayoutGuide.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,17 @@ open class KeyboardLayoutGuide: UILayoutGuide {
// Observe keyboardWillChangeFrame notifications
notificationCenter.addObserver(
self,
selector: #selector(keyboardWillChangeFrame(_:)),
selector: #selector(adjustKeyboard(_:)),
name: UIResponder.keyboardWillChangeFrameNotification,
object: nil
)
// Observe keyboardWillHide notifications
notificationCenter.addObserver(
self,
selector: #selector(adjustKeyboard(_:)),
name: UIResponder.keyboardWillHideNotification,
object: nil
)
}

internal func setUp() {
Expand Down Expand Up @@ -100,7 +107,7 @@ open class KeyboardLayoutGuide: UILayoutGuide {
}

@objc
private func keyboardWillChangeFrame(_ note: Notification) {
private func adjustKeyboard(_ note: Notification) {
if var height = note.keyboardHeight, let duration = note.animationDuration {
if #available(iOS 11.0, *), usesSafeArea, height > 0, let bottom = owningView?.safeAreaInsets.bottom {
height -= bottom
Expand Down Expand Up @@ -142,10 +149,15 @@ extension Notification {
guard let keyboardFrame = userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {
return nil
}
// Weirdly enough UIKeyboardFrameEndUserInfoKey doesn't have the same behaviour
// in ios 10 or iOS 11 so we can't rely on v.cgRectValue.width
let screenHeight = UIApplication.shared.keyWindow?.bounds.height ?? UIScreen.main.bounds.height
return screenHeight - keyboardFrame.cgRectValue.minY

if name == UIResponder.keyboardWillHideNotification {
return 0.0
} else {
// Weirdly enough UIKeyboardFrameEndUserInfoKey doesn't have the same behaviour
// in ios 10 or iOS 11 so we can't rely on v.cgRectValue.width
let screenHeight = UIApplication.shared.keyWindow?.bounds.height ?? UIScreen.main.bounds.height
return screenHeight - keyboardFrame.cgRectValue.minY
}
}

var animationDuration: CGFloat? {
Expand Down

0 comments on commit af93672

Please sign in to comment.