我正在使用带有2个自定义单元格的
UITableView
,其中我有使用UITextView
和UITextField
的自定义单元格,在键盘将编辑的字段隐藏在其上方时,我尝试将其移动到键盘顶部。这是我的viewDidLoad
代码: override func viewDidLoad() {
super.viewDidLoad()
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIKeyboardWillShowNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIKeyboardWillHideNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIKeyboardWillChangeFrameNotification, object: nil)
}
这里是当键盘通知被发送时调用的函数:
func adjustForKeyboard(notification: NSNotification) {
let userInfo = notification.userInfo!
let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let keyboardViewEndFrame = view.convertRect(keyboardScreenEndFrame, fromView: view.window)
if notification.name == UIKeyboardWillHideNotification {
myTableView.contentInset = UIEdgeInsetsZero
print("ZERO")
} else {
myTableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
}
myTableView.scrollIndicatorInsets = myTableView.contentInset
}
对于UITextField
,它可以完美地工作,但是对于UITextView
却不能。为什么呢?