2016-07-09 97 views
1

我正在研究具有註釋和用戶的應用程序,我需要用戶將註釋插入到表格視圖中,我面臨的問題是關於鍵盤的位置,當用戶按下文本時字段來編寫鍵盤出現的註釋和文本字段,如下面的代碼所示。TextFiled鍵盤問題

但問題是,當我更改鍵盤的語言,將鍵盤更改爲表情符號或打開autocorrection覆蓋的文本字段,將不會隨鍵盤佈局移動。

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

回答

0

有一次,我面臨着同樣的問題,並找到了解決如下工作。我不確定這是否是完美的解決方案,但它對我的預期工作。所以你可以試試看。

我所做的是:

  1. 添加UITextInputCurrentInputModeDidChangeNotification通知,viewWillAppear
  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 
         } 
        } 
    } 
    

    希望這將有助於。

+0

感謝其幫助我,但輸精管的方式,但仍然它不是完美的方式,因爲當用戶打開該預測選項提交的文本消失 –

+0

然後有很多可用的庫,它可以處理它。你可以看看https://github.com/michaeltyson/TPKeyboardAvoiding – iRiziya

+0

謝謝iRiziya我會檢查它 –