在我的应用中,我希望收到UIResponder.keyboardWillShowNotification
的通知,以更新我的文本字段的y位置。它在iOS 12之前运行;现在,它在我的一个视图控制器中没有被调用(它在其他控制器中起作用)。这是我执行此操作的代码:
@objc func keyboardWillShow(_ notification: Notification) {
print("keyboard will show 2")
guard let frameValue: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {
return
}
let keyboardFrame = frameValue.cgRectValue
UIView.animate(withDuration: animationTime) {
self.addViewBottomConstraint.constant = keyboardFrame.size.height
self.view.layoutIfNeeded()
print("Bottom contraint height = \(self.addViewBottomConstraint.constant)")
}
}
@objc func keyboardWillHide(_ notification: Notification) {
UIView.animate(withDuration: animationTime) {
self.addViewBottomConstraint.constant = 0
self.view.layoutIfNeeded()
}
}
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
这里,“键盘将显示2”没有被打印,但是为其他具有相同通知的视图控制器打印。 iOS 12中有什么新原因导致此问题吗?否则,是否有不被调用的特定原因?谢谢你的帮助。
我一直在处理相同的问题。我在应用程序的第一个ViewController中运行以下代码。然后,通过获取我们保存在UserDefaults中的键盘高度值,可以在整个应用程序中使用它。它基本上是在屏幕上添加一个文本字段,并在获得键盘高度后立即将其隐藏。
如果您需要根据键盘高度绘制第一个窗口您无需调用t.resignFirstResponder()和t.removeFromSuperview()
这至少适用于iOS 12和13:
var t = UITextField()
let keyboardHeightKey = "keyboardHeight"
override func viewDidAppear(_ animated: Bool) {
let keyboardHeightUserDefault = UserDefaults.standard.double(forKey: keyboardHeightKey)
print("userdefault keyboard height: \(keyboardHeightUserDefault)")
if keyboardHeightUserDefault == 0.0 {
print("keyboard height is 0.0")
keyboardStuff()
}
else {
keyboardHeight = CGFloat(keyboardHeightUserDefault)
print("keyboard height is \(keyboardHeight)")
}
}
@objc func keyboardWillShow(_ notification: NSNotification) {
print("keyboardWillShow")
let kh = UserDefaults.standard.double(forKey: keyboardHeightKey)
if kh == 0.0 {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardH = keyboardSize.height
print("keyboardHeight: \(keyboardH)")
keyboardHeight = keyboardH
UserDefaults.standard.set(keyboardH, forKey: keyboardHeightKey)
UserDefaults.standard.synchronize()
}
}
t.resignFirstResponder()
t.removeFromSuperview()
print("kh is: \(kh)")
}
func keyboardStuff() {
print("inside keyboardStuff")
t = UITextField(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(10), height: CGFloat(10)))
t.delegate = self
self.view.addSubview(t)
let item = t.inputAssistantItem
t.autocorrectionType = .no
item.leadingBarButtonGroups = []
item.trailingBarButtonGroups = []
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
delay(0.1) {
print("inside delay")
t.resignFirstResponder()
t.removeFromSuperview()
}
t.becomeFirstResponder()
t.isHidden = true
}
func delay(_ delay:Double, closure:@escaping ()->()) {
let when = DispatchTime.now() + delay
DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}