TextFiled键盘问题上方

时间:2016-07-09 09:32:21

标签: swift keyboard uitextfield uikeyboard nsnotifications

我正在使用有评论和用户的App,我需要用户将评论插入到表格视图中,我面临的问题是当用户按文本字段写评论时键盘的位置键盘出现,文本字段位于其上方,如下面的代码所示。

但问题是当我更改键盘的语言时,将键盘更改为表情符号或打开文本字段覆盖的自动更正,并且不会随键盘布局移动。

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
        }
    }
}

Screenshot

1 个答案:

答案 0 :(得分:0)

一旦我遇到同样的问题并找到了解决方法如下。我不确定这是否是完美的解决方案,但它对我来说是预期的。所以你可以尝试一下。

我做的是:

  1. UITextInputCurrentInputModeDidChangeNotification
  2. 中添加viewWillAppear通知
  3. 实施changeInputMode方法以确定键盘类型
  4. 处理keyboardWillShowkeyboardWillHide

    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
            }
        }
    }
    

    希望它会有所帮助。