gpt4 book ai didi

ios - Swift UISearchController tableview 搜索结果滚动不会在键盘上方移动到最后一行

转载 作者:行者123 更新时间:2023-11-29 05:16:58 26 4
gpt4 key购买 nike

在我的场景中,我将 Tableview 与 searchController 结合使用。单击搜索按钮后将显示 searchcontroller。在这里,一旦搜索 Controller 通过搜索结果列表时间,如果我滚动我的表格 View ,它不会滚动到底部的最后一个“行”。我需要在键盘上方显示底行。我尝试了下面的代码但不起作用。

单击按钮以显示搜索 Controller

@IBAction func Click_search(_ sender: Any) {
searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.keyboardType = UIKeyboardType.asciiCapable
searchController.searchBar.placeholder = "Search"
searchController.searchBar.backgroundColor = UIColor.red
searchController.searchBar.tintColor = UIColor.white
let cancelButtonAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes , for: .normal)
searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
present(searchController, animated: true, completion: nil)
}

ViewDidload 内部

  NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)

键盘隐藏和显示以调整 Tablview 高度

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

@objc private func keyboardWillHide(notification: NSNotification) {
tableView.contentInset = .zero
}

最佳答案

试试这个代码。您正在滚动 TableView。请尝试滚动您的 super View 。它会起作用的。尝试让我知道。

在 Click_search 操作中添加以下代码。

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboard(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboard(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboard(notification:)), name:UIResponder.keyboardWillChangeFrameNotification, object: nil)

调用此函数来检查并将 tableView 向上滚动。//标记:- 键盘检查

 @objc func keyboard(notification:Notification) {
if notification.name == UIResponder.keyboardWillShowNotification || notification.name == UIResponder.keyboardWillChangeFrameNotification {
self.view.frame.origin.y = -15
}else{
self.view.frame.origin.y = 0
}
}

关于ios - Swift UISearchController tableview 搜索结果滚动不会在键盘上方移动到最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59087264/

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