gpt4 book ai didi

ios - keyboardWillShowNotification 在被解雇时调用了两次

转载 作者:行者123 更新时间:2023-11-28 07:46:43 36 4
gpt4 key购买 nike

我已订阅 keyboardWillShowNotificationkeyboardWillHideNotification 以在我的 UI 中移动。我注意到,当我通过点击“Go”按钮关闭键盘时,keyboardWillShowNotification 被调用两次(因此重置了我的一些约束)但是如果通过点击键盘上的 return 关闭键盘(MacBook)然后它没有被调用两次。

如何避免它被调用两次?为什么这种行为甚至存在?我找不到任何关于它的提及(很多关于它被输入 View 调用两次的引用......等)但在被解雇时从来没有。

这是我的代码:

override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardNotification(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWasDismissed(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}

和...

@objc func keyboardNotification(notification: NSNotification) {
guard
let animationDuration = notification.userInfo?["UIKeyboardAnimationDurationUserInfoKey"] as? Double,
let animationCurve = notification.userInfo?["UIKeyboardAnimationCurveUserInfoKey"] as? NSNumber,
let frameEnd = notification.userInfo?["UIKeyboardFrameEndUserInfoKey"] as? CGRect,
let frameBegin = notification.userInfo?["UIKeyboardFrameBeginUserInfoKey"]
else {
print("No userInfo recived from NSNotification.Name.UIKeyboardWillShow")
return
}
print("WILL SHOW")

let margin = self.view.safeAreaLayoutGuide
constraintsWhenKeyboardVisible = [
boxOffice.leadingAnchor.constraint(equalTo: margin.leadingAnchor),
boxOffice.trailingAnchor.constraint(equalTo: margin.trailingAnchor),
boxOffice.bottomAnchor.constraint(equalTo: margin.bottomAnchor),
boxOffice.topAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -(frameEnd.height + 50))
]
NSLayoutConstraint.deactivate(boxOfficeFinalConstraints)
NSLayoutConstraint.activate(constraintsWhenKeyboardVisible)

UIView.animate(withDuration: animationDuration,
delay: TimeInterval(0),
options: UIView.AnimationOptions(rawValue: animationCurve.uintValue),
animations: {
self.boxOffice.answerField.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
self.view.layoutIfNeeded()
},
completion: nil)
}


@objc func keyboardWasDismissed(notification: NSNotification) {
guard
let animationDuration = notification.userInfo?["UIKeyboardAnimationDurationUserInfoKey"] as? Double,
let animationCurve = notification.userInfo?["UIKeyboardAnimationCurveUserInfoKey"] as? NSNumber
else {
print("No userInfo recived from NSNotification.Name.UIKeyboardWillShow")
return
}
print("WILL HIDE")
//print(notification)
NSLayoutConstraint.deactivate(self.constraintsWhenKeyboardVisible)
NSLayoutConstraint.activate(self.boxOfficeFinalConstraints)

UIView.animate(withDuration: animationDuration,
delay: TimeInterval(0),
options: UIView.AnimationOptions(rawValue: animationCurve.uintValue),
animations: {
self.boxOffice.answerField.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMaxXMinYCorner, .layerMinXMaxYCorner, .layerMinXMinYCorner]
self.view.layoutIfNeeded()
},
completion: nil)

}

最佳答案

我还没有解决 keyboardWillShowNotification 在 iOS 键盘模拟器上按下 Return 而不是在模拟器的硬件键盘上被发布的问题,但我已经修改了我的代码,以便在显示键盘时我不会添加约束,我只是使用键盘通知中的高度修改约束的常量。这已经解决了。

boxOfficeWhenKeyboardVisible[3].constant = -(frameEnd.height + 50)

关于ios - keyboardWillShowNotification 在被解雇时调用了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50792104/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com