Ответ 1
Уже поздно, но UIKeyboardWillChangeFrameNotification
- это то, чего ты хочешь.
Я пытаюсь переместить представление вверх, когда клавиатура выскакивает над UITextfield
, который помещается на UIScrollView
. Для этого я использую UIKeyboardWillShowNotification
и UIKeyboardWillHideNotification
.
Он отлично работает при использовании iOS Keyboard
, где я получаю высоту 297
.
Мой клиент использует Gboard keyboard
, он жаловался, что представление не движется. Когда я тестировал, я получаю высоту клавиатуры как 44
.
Я пробовал оба ключа UIKeyboardFrameBeginUserInfoKey
и UIKeyboardFrameEndUserInfoKey
для объекта NSNotifiction userInfo
. Оба дают только 44
.
Я пробовал с UIKeyboardDidShowNotification
и UIKeyboardDidHideNotification
также, еще одну проблему.
Может ли кто-нибудь помочь мне в этом...?
Уже поздно, но UIKeyboardWillChangeFrameNotification
- это то, чего ты хочешь.
НЕ ОТВЕТ, но только здесь b/c его слишком долго, чтобы вставить комментарий. вот мой код, чтобы получить высоту клавиатуры @iMHitesh:
class KeyboardShiftAnimator {
fileprivate let showAnimation: (_ keyboardHeight: CGFloat) -> Void
fileprivate let hideAnimation: (Void) -> Void
fileprivate weak var ownerController: UIViewController!
fileprivate let disableAnimations: Bool
deinit {
NotificationCenter.default.removeObserver(self)
}
init(ownerController: UIViewController, disableAnimations: Bool = false, showAnimation: @escaping (_ keyboardHeight: CGFloat) -> Void, hideAnimation: @escaping (Void) -> Void) {
self.showAnimation = showAnimation
self.hideAnimation = hideAnimation
self.ownerController = ownerController
self.disableAnimations = disableAnimations
NotificationCenter.default.addObserver(
self,
selector:
#selector(KeyboardShiftAnimator.keyboardWillShow(_:)),
name: NSNotification.Name.UIKeyboardWillShow,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector:
#selector(KeyboardShiftAnimator.keyboardWillHide(_:)),
name: NSNotification.Name.UIKeyboardWillHide,
object: nil
)
}
func makeAppropriateLayout(_ duration: NSNumber) {
if self.disableAnimations {
UIView.performWithoutAnimation({
self.ownerController.view.setNeedsUpdateConstraints()
self.ownerController.view.updateConstraintsIfNeeded()
self.ownerController.view.layoutIfNeeded()
})
} else {
self.ownerController.view.setNeedsUpdateConstraints()
self.ownerController.view.updateConstraintsIfNeeded()
UIView.animate(withDuration: duration.doubleValue, animations: { () -> Void in
self.ownerController.view.layoutIfNeeded()
})
}
}
@objc func keyboardWillShow(_ note: Foundation.Notification) {
guard let endFrameValue = (note as NSNotification).userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue,
let animationDuration = (note as NSNotification).userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber
else { return }
let keyboardHeight = endFrameValue.cgRectValue.size.height
showAnimation(keyboardHeight)
makeAppropriateLayout(animationDuration)
}
@objc func keyboardWillHide(_ note: Foundation.Notification) {
let animationDuration = (note as NSNotification).userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber
let duration = animationDuration != nil ? animationDuration!.doubleValue : 0.25
hideAnimation()
makeAppropriateLayout(NSNumber(value: duration))
}
}