gpt4 book ai didi

ios - 您可以多次设置 contentInset 吗?

转载 作者:搜寻专家 更新时间:2023-11-01 06:56:03 25 4
gpt4 key购买 nike

我有一个带有表格单元格的表格 View 。每个单元格都有一个文本字段。我有以下代码来防止底部的几个单元格被键盘挡住

@objc func keyboardWillShow(_ notification:Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
}
}
@objc func keyboardWillHide(notification: NSNotification) {
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
override func viewDidLoad() {
super.viewDidLoad()

NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillHide), name: UIApplication.keyboardWillChangeFrameNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillShow), name: UIApplication.keyboardWillChangeFrameNotification, object: nil)
}

keyboardWillHide 函数按预期工作。但是,当隐藏键盘时,表格会弹回底部,导致不显示额外的空白(这正是我想要的)。我不喜欢的是您仍然可以在表格上向下滚动到首次显示键盘的位置的 contentInset。有没有办法让键盘消失后,你不能向下滚动到表格底部?

最佳答案

替换

keyboardFrameBeginUserInfoKey

keyboardFrameEndUserInfoKey

@objc func keyboardWillShow(_ notification:Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
}
}
@objc func keyboardWillHide(notification: NSNotification) {

tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillHide), name: UIApplication.keyboardWillHideNotification, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillShow), name: UIApplication.keyboardWillShowNotification, object: nil)
}

关于ios - 您可以多次设置 contentInset 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53270396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com