我有下面的例子来说明这个问题.
我主要想展示一个类似于iMessage的用户界面,其中底部有一个用于输入消息的UITextView.
一旦键盘显示在UITextView中,即使稍微滚动UITableView,内容也会位于导航栏下方,而导航栏保持透明.这看起来像是一个奇怪的用户界面错误.
import UIKit
import SnapKit
let numberOfRows = 15
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var myTableView: UITableView!
let textView = UITextView()
let bottomView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
myTableView.keyboardDismissMode = .interactive
bottomView.backgroundColor = .darkGray
bottomView.addSubview(textView)
view.addSubview(bottomView)
bottomView.snp.makeConstraints { make in
make.left.right.bottom.equalToSuperview()
make.height.equalTo(100)
}
textView.backgroundColor = .black
textView.snp.makeConstraints { make in
make.edges.equalToSuperview().inset(10)
}
textView.text = "Type something.."
}
override var inputAccessoryView: UIView {
return bottomView
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numberOfRows
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
cell.textLabel?.text = "Row \(indexPath.row)"
cell.detailTextLabel?.text = "Tastes very close to Snickers chocolate bar. Only tried because of reviews. Would purchase again if price wasn’t so high, and available in Canada."
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
navigationController?.pushViewController(UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DetailVC"), animated: true)
}
}
屏幕截图后的错误滚动后,甚至一点点键盘显示: