【问题标题】:TextFiled above the Keyboard issue键盘问题上方的文本字段
【发布时间】: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


    【解决方案1】:

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

    我所做的是:

    1. viewWillAppear中添加UITextInputCurrentInputModeDidChangeNotification通知
    2. 实现changeInputMode 方法来确定键盘类型
    3. 处理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
              }
          }
      }
      

      希望它会有所帮助。

    【讨论】:

    • 感谢它帮助了我,但以不同的方式帮助我,但仍然不是完美的方式,因为当用户打开预测选项时,文本文件消失
    • 那么有很多库可以处理它。你可以看看github.com/michaeltyson/TPKeyboardAvoiding
    猜你喜欢
    • 2016-07-07
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多