我做了30个文本框,并var scrollview: UIScrollView
在viewDidLoad()
和我试图拉升整体向上滚动视图时,键盘盖文本框。Delegate
的文本字段和scrollview已经设置。此代码(下面向下)有效,并且第一次出现键盘时scrollview会向上移动,但是问题是,当第二次或更多次打开键盘时,scrollview不会向上移动,我一直无法解决问题几个小时。谁能在我的代码中检测到一些错误并给出正确答案?我期待任何帮助!
我写的代码是这样的:
var activeField: UITextField?
func registerForKeyboardNotifications(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notificaiton:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func deregisterForKeyboardNotifications(){
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name:NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@objc func keyboardWasShown(notificaiton: NSNotification){
var info = notificaiton.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)
var aRect: CGRect = view.frame
aRect.size.height -= keyboardSize!.height
if let activeField = activeField{
if !aRect.contains(activeField.frame.origin) == …
Run Code Online (Sandbox Code Playgroud)