UITableViewの一つのUITableViewCellの中にUITextViewを配置しているのですが、Keyboardが表示されるとUITextViewが隠れてしまいます。他にもUITextFieldを置いているのですが、そちらはきちんとキーボードの上まで表示されます。
キーボードの表示に追随するコードはこちらです。
swift
1 func setupKeyboardNotification() { 2 NotificationCenter.default.addObserver(self, 3 selector: #selector(self.keyboardWillShow), 4 name: UIResponder.keyboardWillShowNotification, 5 object: nil) 6 NotificationCenter.default.addObserver(self, 7 selector: #selector(self.keyboardWillHide), 8 name: UIResponder.keyboardWillHideNotification, 9 object: nil) 10 } 11 12 @objc private func keyboardWillShow(_ notification: NSNotification?) { 13 guard 14 let userInfo = notification?.userInfo, 15 let rect = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect, 16 let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double else { 17 return 18 } 19 let bottomMargin = 50.0 20 21 self.bottomConstraint.constant = -rect.size.height + bottomMargin 22 UIView.animate(withDuration: duration, animations: { 23 self.view.layoutIfNeeded() 24 }) 25 } 26 27 @objc private func keyboardWillHide(_ notification: NSNotification?) { 28 guard 29 let userInfo = notification?.userInfo, 30 let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double else { 31 return 32 } 33 self.bottomConstraint.constant = 0 34 UIView.animate(withDuration: duration, animations: { 35 self.view.layoutIfNeeded() 36 }) 37 }
UITextViewがキーボードの上に表示されるようにするにはどのように修正すればよろしいでしょうか?
keyboardWillShow で self.view のサイズが変わり、それに合わせて tableView のサイズも変わるようになってると思いますが、その際にスクロール位置は変わらないために隠れてしまうのでは。サイズ変更後に、現在編集中のセルが見える位置に来るように scrollToRow メソッドでスクロールさせれば良いかも。
https://developer.apple.com/documentation/uikit/uitableview/1614997-scrolltorow
あなたの回答
tips
プレビュー