键盘弹出时如何向上移动整个视图?(迅速)

elv*_*lkm 5 swift

在此处输入图片说明

底部的灰色框是文本视图。当我点击文本视图时,键盘将从底部弹出。但是,文本视图已被弹出键盘覆盖。

当键盘弹出时,我应该添加什么功能以使整个视图向上移动?

mil*_*526 2

要检测键盘何时出现,您可以听NSNotificationCenter

\n\n
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)\nNSNotificationCenter.defaultCenter().addObserver(self, selector: \xe2\x80\x9ckeyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)\n
Run Code Online (Sandbox Code Playgroud)\n\n

这将调用keyboardWillShowkeyboardWillHide。在这里你可以用你的UITextfield

\n\n
func keyboardWillShow(notification: NSNotification) {\n    if let userInfo = notification.userInfo {\n        if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {\n            //use keyboardSize.height to determine the height of the keyboard and set the height of your textfield accordingly\n        }\n    }\n}\n\nfunc keyboardWillHide(notification: NSNotification) {\n    //pull everything down again\n}\n
Run Code Online (Sandbox Code Playgroud)\n