【发布时间】:2016-11-11 19:58:15
【问题描述】:
我正在开发具有 cmets 和用户的应用程序,我需要用户将评论插入到表格视图中,我面临的问题是用户按下文本字段写评论时的键盘键盘出现,文本字段位于其上方,如下代码所示。
但问题是当我更改键盘的语言,将键盘更改为表情符号或打开自动更正覆盖的文本字段并且不会随键盘布局移动时。
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// KeyBoard Show and Hide
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(Commants_Page.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object:nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(Commants_Page.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self,selector: #selector(Commants_Page.adjustForKeyboard(_:)),name: UIKeyboardWillChangeFrameNotification,object: nil)
}
// KeyBoard Show and Hide Function
func keyboardWillShow(notification: NSNotification) {
if KeyBoardMove == false {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
self.view.frame.origin.y -= keyboardSize.height
KeyBoardMove = true
}
}
}
func keyboardWillHide(notification: NSNotification) {
if KeyBoardMove == true {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
self.view.frame.origin.y += keyboardSize.height
KeyBoardMove = false
}
}
}
【问题讨论】:
标签: swift keyboard uitextfield uikeyboard nsnotifications