导航栏和第一个 UITableViewCell 之间的差距

Posted

技术标签:

【中文标题】导航栏和第一个 UITableViewCell 之间的差距【英文标题】:Gap between navigation bar and first UITableViewCell 【发布时间】: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/sea​​rchBar,但仍然没有运气。让我猜一下,你是在 useDefaultPageLoadingIndicator() 或 setBackgounrdViewIfEmpty() 中添加 tableHeaderView 还是一些 Insect? Ahhhh 愚蠢的我,在浏览您的代码时没有想到 tableView 样式。很高兴听到你修好了它。祝你有美好的一天;) 【参考方案1】:

该问题是由于之前的视图控制器上的表格视图样式设置为.grouped

将样式设置为.plain 解决了这个问题。

我认为分组样式导致空白的原因是因为它在单元格上方添加了一个空白部分标题。

【讨论】:

以上是关于导航栏和第一个 UITableViewCell 之间的差距的主要内容,如果未能解决你的问题,请参考以下文章

Android 显示、隐藏状态栏和导航栏

swift 需求: 导航栏和HeaderView 使用一个背景图片。

带有 TextField 和第一响应者的自定义 UITableViewCell

UITableView:向上滑动时收缩标签栏和导航栏

隐藏标签栏和导航栏

棒棒糖上完全透明的状态栏和导航栏