2016-05-22 5 views
1

スウィフトを使用して、キーボードの高さの変更、またはiOSのキーボードの変更を検出する方法。それによるとキーボード高さ変更オブザーバ

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CommentView.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil) 
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CommentView.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil) 

と私は私のボタンの位置を変更しています:私はキーボードの天気を検出するために、私のアプリのためにオブザーバーを追加することができるよ

はショーや使用しない

func keyboardWillShow(notification: NSNotification) { 
     animateTextFieldWithKeyboard(notification) 
} 

func keyboardWillHide(notification: NSNotification) { 
     animateTextFieldWithKeyboard(notification) 
} 

func animateTextFieldWithKeyboard(notification: NSNotification) { 


let userInfo = notification.userInfo! 

let keyboardSize = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() 
let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double 
let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as! UInt 

// baseContraint is your Auto Layout constraint that pins the 
// text view to the bottom of the superview. 

if notification.name == UIKeyboardWillShowNotification { 
    if (BottomConstraint.constant == 0) { 
     BottomConstraint.constant += keyboardSize.height 
    } 

    // move up 
} 
else { 
    BottomConstraint.constant = 0 
    // move down 
} 

view.setNeedsUpdateConstraints() 

let options = UIViewAnimationOptions(rawValue: curve << 16) 
UIView.animateWithDuration(duration, delay: 0, options: options, 
    animations: { 
     self.view.layoutIfNeeded() 
    }, 
    completion: nil 
) 

} 

あなたはスクリーンショットで見ることができるようにすべてが正常に動作している:

enter image description here

ただし、キーボードの種類を絵文字などに変更すると問題が発生します。それは私のテキストフィールドと私のボタンを隠しますので、私はあなたがに取得XcodeのドキュメントにUIKeyboardWillShowNotificationで検索する場合は、キーボード新しい高さ

enter image description here

+0

こんにちは、この問題の解決策を見つけましたか、今この問題に直面しています。提案は歓迎です – karthik

答えて

1

に応じてボタンの位置とTextFiendを変更したいですUIWindowのセクション。最後に通知のテーブルがあります。

私はUIKeyboardWillChangeFrameNotificationを試してみることをお勧めします。

答えを探す時間:約30秒。

+0

あなたの答えはありがとうございます、私はそれを試してみると、私はあなたに戻ってきます。 –

+0

この機能を試してみると、変更前のキーボードの高さが分かりました。 =。=すべての更新? –

+0

動作しません。キーボード入力が英語または他の言語から絵文字に変わると、通知はトリガーされません。 正解を見つける時間ですか?お知らせいたします。 – Fmessina

0

これを使用すると、すべての通知が表示されます。

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil) 

}  

@objc func keyboardWillShow(notification: NSNotification) { 
    let keyboardSize = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue 

     if keyboard == false{ 
      keyboard = true 
      lastKeyboardHeight = keyboardSize.height 
      chatDetailView.frame.origin.y = chatDetailView.frame.origin.y-(keyboardSize.height-bottomMenu.frame.height) 
     } 
    } 

    @objc func keyboardWillChange(notification: NSNotification) { 
     let keyboardSize1 = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue 

     if keyboard == true && lastKeyboardHeight != keyboardSize1.height { 
      if lastKeyboardHeight < keyboardSize1.height{ 
       let keyboardDifference: CGFloat = keyboardSize1.height-lastKeyboardHeight 
       chatDetailView.frame.origin.y -= keyboardDifference 

      } else { 
       let keyboardDifference: CGFloat = lastKeyboardHeight-keyboardSize1.height 
       chatDetailView.frame.origin.y += keyboardDifference 
      } 
      lastKeyboardHeight = keyboardSize1.height 
     } 
    } 

    @objc func keyboardWillHide(notification: NSNotification) { 
     if keyboard == true { 
      keyboard = false 
      chatDetailView.frame.origin.y = chatDetailView.frame.origin.y+(lastKeyboardHeight-bottomMenu.frame.height) 
     } 
    } 
関連する問題