Ios 为什么在swift中向上移动键盘时,视图和滚动视图之间会出现间隙?
我安排了如下视图 我的差距如下,为什么? 当视图下降时,它看起来很好,当我点击文本字段时,总视图上升,当我浏览tableview时,我得到了如下之间的间隙 这是我的代码:Ios 为什么在swift中向上移动键盘时,视图和滚动视图之间会出现间隙?,ios,swift,scroll,keyboard,Ios,Swift,Scroll,Keyboard,我安排了如下视图 我的差距如下,为什么? 当视图下降时,它看起来很好,当我点击文本字段时,总视图上升,当我浏览tableview时,我得到了如下之间的间隙 这是我的代码: import UIKit class ChatViewController: UIViewController, UITextFieldDelegate { @IBOutlet weak var belowtextFieldHeightConstraint: NSLayoutConstraint! @
import UIKit
class ChatViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var belowtextFieldHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var catViewBottomConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboard(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboard(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func keyboard(notification:Notification) {
guard let keyboardReact = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
return
}
if notification.name == UIResponder.keyboardWillShowNotification || notification.name == UIResponder.keyboardWillChangeFrameNotification {
self.view.frame.origin.y = -keyboardReact.height
} else {
self.view.frame.origin.y = 0
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
文本字段有一个名为
inputAccessoryView
的属性,请确保您没有在任何地方设置视图,并且在调用键盘时,该视图为nil
看起来您已经设置了一个视图。文本字段有一个名为
inputAccessoryView
的属性,请确保您没有在任何地方设置视图,并且在调用键盘时,该视图为nil
看起来你已经准备好了