我正在动态构建一个表单,其中每个 UITableViewCell
都有各种类型的 UI 元素。
有一个 UITableViewCell
包含一个 UITextView
并且我希望能够在显示键盘时保持可见性。我看过其他类似的问题,但一直找不到解决方案。
我写的Swift版本推荐的是:Apple's Managing Keyboard
它不起作用有两个原因。
1.) 键盘通知在 TextViewWillBeginEditing
之前触发。
2.) UITextView
的框架与UITableViewCell
的父 View 有关,所以检查是错误的。
这是我当前的代码:
func adjustTableViewForKeyboard(notification: NSNotification) {
let userInfo = notification.userInfo!
let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let keyboardViewEndFrame = view.convertRect(keyboardScreenEndFrame, fromView: view.window)
if notification.name == UIKeyboardWillHideNotification {
self.tableView.contentInset = UIEdgeInsetsZero
} else {
self.tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
rect = self.view.frame;
rect!.size.height -= keyboardViewEndFrame.height
}
self.tableView.scrollIndicatorInsets = self.tableView.contentInset
}
func textViewDidBeginEditing(textView: UITextView) {
self.activeTextView = textView;
if(activeTextView != nil){
// This check does NOT work due to the TextView's superview is the cell.
if(!CGRectContainsPoint(rect!, (activeTextView?.frame.origin)!)){
self.tableView.scrollRectToVisible((activeTextView?.frame)!, animated: true)
}
}
}
这在能够滚动到所有单元格方面有效,但我还想确保 UITextView
未被键盘隐藏。
最佳答案
您可以使用响应链来解决这个问题。在您的通知处理程序中,尝试在您的 TextView 上调用 isFirstResponder()
;如果它返回 true
,那么您可以从那里调用表格 View 滚动代码。
关于ios - 显示键盘时保持 UITableView 中聚焦的 UITextView 的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37176595/