【发布时间】:2016-09-07 16:17:22
【问题描述】:
我正在动态构建一个表单,其中每个 UITableViewCell 都有各种类型的 UI 元素。
有一个UITableViewCell 包含一个UITextView,我希望在显示键盘时能够保持可见性。我查看了其他类似的问题,但无法找到解决方案。
我写了 Swift 版本的推荐者:Apple's Managing Keyboard
它不起作用有两个原因。
1.) 在TextViewWillBeginEditing 之前触发键盘通知。
2.) UITextView 的框架与UITableViewCell 的父视图相关,所以检查错误。
这是我当前的代码:
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 不会被键盘隐藏。
【问题讨论】:
标签: ios swift uitableview keyboard