这是我的代码。
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
@objc func keyboardWillShow(notification: NSNotification){
guard let userInfo = notification.userInfo else {return}
guard let keyboardSize = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue else {return}
let keyboardFrame = keyboardSize.cgRectValue
if (self.view.frame.origin.y == 0) {
self.view.frame.origin.y -= keyboardFrame.height
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if (self.view.frame.origin.y != 0) {
self.view.frame.origin.y = 0
}
}
有没有一种方法可以使textview而不是整个viewcontroller向上移动?