我有以下示例显示了该问题。
我基本上想显示一个类似于 iMessage 的 UI,其中 UITextView 位于底部用于输入消息。
一旦键盘显示在 UITextView 中,即使稍微滚动 UITableView,内容也会进入导航栏下方,而导航栏保持透明。这似乎是一个奇怪的 UI 错误。
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)
}
}
显示键盘后滚动一点点后的错误屏幕截图:
这真是一件奇怪的事情。然而,我发现,这是因为
edgesForExtendedLayout
。默认情况下会自动 Under Top Bars
和 Under Bottom Bars
。尝试删除这些配置。
edgesForExtendedLayout = []