2016-07-27 7 views
0

私はチャットアプリで作業しており、ユーザーにキーボードを対話的に却下させようとしていました。私はそれを予想通り残念ながら、それは働いていません。キーボードを閉じるためのスムーズな切り替え

image

(これはダウンキーボードを引っ張って始めて私と一緒にいる私はどんなキーボードの上に滞在しないように私のテキストビューをしたいと思います。ここで私が試したものです:viewDidLoad

同じクラスで
textView.keyboardDismissMode = .interactive//I have tried the same thing with the table view as well, but it produces the same result. 

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 

func keyboardWillShow(_ aNotification: NSNotification) { 
    let info = aNotification.userInfo! 
    let keyboardFrame = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue() 
    UIView.animate(withDuration: 0.25) {() -> Void in 
     self.messageBottomConstraint.constant = keyboardFrame.height 
     self.view.layoutIfNeeded() 

    } 
} 

func keyboardWillHide(_ aNotification: NSNotification) { 
    UIView.animate(withDuration: 0.25) {() -> Void in 
     self.messageBottomConstraint.constant = 0 
     self.view.layoutIfNeeded() 

    } 
} 

ユーザーがキーボードを消している間にキーボードの上部にテキストビューを保存するにはどうすればよいですか?ありがとう!

答えて

1

inputAccessoryViewまたはinputAccessoryViewControllerプロパティをUIResponder(これはUIViewControllerから継承)で上書きする必要があります。これにより、表示されたときにキーボードの上部に自動的にピンするビュー/ビューコントローラを提供することができます。

This blog postは、これを行う方法の例を示します。

+0

例を教えてください。ブログの投稿はObj-Cで、私はそれを変換するのに少し苦労しています...ありがとう! – penatheboss

関連する問題