键盘问题上方的文本字段
TextFiled above the Keyboard issue
我正在开发有评论和用户的应用程序,我需要用户将评论插入 table 视图,我面临的问题是关于键盘,当用户按下用于编写评论的文本字段,键盘出现,文本字段位于其上方,如下面的代码。
但问题是当我更改键盘语言、将键盘更改为表情符号或打开自动更正时,文本字段被覆盖并且不会随键盘布局移动。
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
}
}
}
有一次我遇到了同样的问题并找到了如下解决方法。我不确定这是否是完美的解决方案,但它按我的预期工作。所以你可以试试看。
我做的是:
- 在
viewWillAppear
中添加UITextInputCurrentInputModeDidChangeNotification
通知
- 执行
changeInputMode
方法来确定键盘类型
处理 keyboardWillShow
和 keyboardWillHide
var isEmoji = Bool()
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
isEmoji = false
NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeInputMode:", name:UITextInputCurrentInputModeDidChangeNotification, object: nil)
}
func changeInputMode(notification : NSNotification)
{
//Emoji keyboard does not have any primary language whereas normal text keyboard does have. Therefore, on the bases of it we can determine the keyboard type
if let lang = txtComment.textInputMode?.primaryLanguage
{
isEmoji = false
}
else{
isEmoji = true
}
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
print(isEmoji)
if(!isEmoji){
writeCommentView.frame.origin.y -= keyboardSize.height
}
else{
writeCommentView.frame.origin.y -= 37 //where 37 is the height differece between normal keyboard and emoji keyboard. Change this value accordingly
}
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
//Handle this method accordingly. For example..
if(!isEmoji){
writeCommentView.frame.origin.y += keyboardSize.height
}
else{
writeCommentView.frame.origin.y += 37
}
}
}
希望对您有所帮助。
我正在开发有评论和用户的应用程序,我需要用户将评论插入 table 视图,我面临的问题是关于键盘,当用户按下用于编写评论的文本字段,键盘出现,文本字段位于其上方,如下面的代码。
但问题是当我更改键盘语言、将键盘更改为表情符号或打开自动更正时,文本字段被覆盖并且不会随键盘布局移动。
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
}
}
}
有一次我遇到了同样的问题并找到了如下解决方法。我不确定这是否是完美的解决方案,但它按我的预期工作。所以你可以试试看。
我做的是:
- 在
viewWillAppear
中添加UITextInputCurrentInputModeDidChangeNotification
通知 - 执行
changeInputMode
方法来确定键盘类型 处理
keyboardWillShow
和keyboardWillHide
var isEmoji = Bool() override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) isEmoji = false NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeInputMode:", name:UITextInputCurrentInputModeDidChangeNotification, object: nil) } func changeInputMode(notification : NSNotification) { //Emoji keyboard does not have any primary language whereas normal text keyboard does have. Therefore, on the bases of it we can determine the keyboard type if let lang = txtComment.textInputMode?.primaryLanguage { isEmoji = false } else{ isEmoji = true } } func keyboardWillShow(notification: NSNotification) { if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() { print(isEmoji) if(!isEmoji){ writeCommentView.frame.origin.y -= keyboardSize.height } else{ writeCommentView.frame.origin.y -= 37 //where 37 is the height differece between normal keyboard and emoji keyboard. Change this value accordingly } } } func keyboardWillHide(notification: NSNotification) { if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() { //Handle this method accordingly. For example.. if(!isEmoji){ writeCommentView.frame.origin.y += keyboardSize.height } else{ writeCommentView.frame.origin.y += 37 } } }
希望对您有所帮助。