iOS 11 TableViewCell 内嵌 TextView 的一些问题

1、键盘遮挡问题

在 iOS 10 中,当 TableViewCell 内嵌的 TextView 成为第一响应者弹出键盘后,输入的内容有多行时,TableView 会自动向上移动,使得 TextView 内容不被键盘遮挡。

但在 iOS 11 中,这个特性消失了,多行内容在输入时会被键盘遮挡,TableView 也不会自动向上移动。

解决方法:

 textView.textContainer.maximumNumberOfLines = 0

 func textViewDidChange(_ textView: UITextView) {
 
       if #available(iOS 11, *) {
           UIView.performWithoutAnimation {
               tableView?.performBatchUpdates(nil, completion: nil)
               textView.sizeToFit()
           }
       }
       else {
           tableView?.beginUpdates()
           tableView?.endUpdates()
      }
   }

2、输入文字时,TableView 跳动的问题

iOS 11 当 TableViewCell 内嵌的 TextView 成为第一响应者弹出键盘后,输入内容会出现 TableView 不停跳跃的问题。

解决方法:
iOS 10 中这样动态更新 TextView 高度:

tableView?.beginUpdates()
tableView?.endUpdates()

iOS 11 中改为如下代码:

UIView.performWithoutAnimation {
      tableView?.performBatchUpdates(nil, completion: nil)
      textView.sizeToFit()
}

3、tableView 滚动时,退下键盘

tableView.keyboardDismissMode = .onDrag
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容