Created
October 4, 2020 14:55
-
-
Save prafullakumar/c914a3676b4b52cce320b8145f15d41e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class KeyboardResponder: ObservableObject { | |
private var notificationCenter: NotificationCenter | |
@Published private(set) var currentHeight: CGFloat = 0 | |
init(center: NotificationCenter = .default) { | |
notificationCenter = center | |
notificationCenter.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil) | |
notificationCenter.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil) | |
} | |
func dismiss() { | |
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) | |
} | |
deinit { | |
notificationCenter.removeObserver(self) | |
} | |
@objc func keyBoardWillShow(notification: Notification) { | |
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue { | |
currentHeight = keyboardSize.height | |
} | |
} | |
@objc func keyBoardWillHide(notification: Notification) { | |
currentHeight = 0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment