Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve a rare problem where keyboard height will be full-screen on dismissal #39

Merged
merged 1 commit into from
Jan 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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