【发布时间】:2020-09-03 01:02:29
【问题描述】:
我有一个 UITableViewController,导航栏中嵌入了一个搜索控制器。
当视图以模态显示时,第一行和导航栏之间存在间隙。
这是我的实现。为保密起见,已对某些代码进行了编辑。但是关于视图控制器设置的任何内容都应该存在:
import Foundation
class SearchViewController: UITableViewController {
fileprivate lazy var searchController: UISearchController = {
let searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.showsCancelButton = true
searchController.searchBar.delegate = self
return searchController
}()
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
navigationItem.titleView = searchController.searchBar
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.navigationBar.tintColor = BrandColor.appleDarkGray
}
}
extension ExploreSearchViewController: UISearchBarDelegate {
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
presentingViewController?.dismiss(animated: true, completion: nil)
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if let searchText = searchBar.text {
tableView.refreshControl?.beginRefreshing()
searchedText = searchText
}
}
}
// MARK: - UITableView Methods
extension ExploreSearchViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchResultsProvider.searchResults.count
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard searchResultsProvider.canLoadNextPage else { return }
// handle paging here...
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard searchResultsProvider.searchResults.count > indexPath.row else { return UITableViewCell() }
// Setup search result cell here...
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Handle cell did tap here...
}
@objc private func refresh() {
searchResultsProvider.refreshResults()
}
// MARK - Table View Helpers
private func setupTableView() {
registerCells()
self.tableView.backgroundColor = .white
self.tableView.contentInsetAdjustmentBehavior = .always
self.tableView.refreshControl = UIRefreshControl()
self.tableView.refreshControl?.addTarget(self, action: #selector(refresh), for: .valueChanged)
self.tableView.useDefaultPageLoadingIndicator()
self.tableView.setBackgroundViewIfEmpty(message: Self.searchInitialMessage, remedyButton: nil)
}
private func registerCells() {
//Register Table View Cells here...
}
}
以前有人见过这种问题吗?我无法向上移动顶部边缘插图,因为刷新控件将隐藏在导航栏后面。
以前有人见过这种行为吗?如果有,你是怎么解决的?
【问题讨论】:
-
能否请您展示一些有关如何实现 tableview 的代码?由于提供的信息有限,无法真正重现该问题...
-
你试过
tableView.contentInsetAdjustmentBehavior = .never吗?有帮助吗? -
@paky 抱歉。用我的代码编辑了帖子。一些代码已被编辑。但是关于表格视图、搜索控制器和视图设置的一切都在那里。
-
仍然无法从提供的代码中重现差距,我尝试使用情节提要和编程方式添加 navigationController/tableView/searchBar,但仍然没有运气。让我猜一下,你是在 useDefaultPageLoadingIndicator() 或 setBackgounrdViewIfEmpty() 中添加 tableHeaderView 还是一些 Insect?
-
Ahhhh 愚蠢的我,在浏览您的代码时没有想到 tableView 样式。很高兴听到你修好了它。祝你有美好的一天;)
标签: swift uitableview uisearchcontroller