【问题标题】:Extra space on top of UISearchController's UITableViewUISearchController 的 UITableView 顶部的额外空间
【发布时间】:2016-11-05 07:24:01
【问题描述】:

我这样设置UISearchController

    searchResultsController = SearchResultsController()

    searchController = UISearchController(searchResultsController: searchResultsController)
    searchController.searchResultsUpdater = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.dimsBackgroundDuringPresentation = true
    searchController.searchBar.searchBarStyle = .minimal
    searchController.delegate = self
    definesPresentationContext = true

    navigationItem.titleView = searchController.searchBar

SearchResultsController 只是一个带有UITableView 的视图控制器,用于显示搜索结果。

当设备处于竖屏状态时,以竖屏模式开始搜索,表格视图顶部有一个额外的空间(RESULT 标题是表格视图的开始)

然后如果我旋转到横向模式,空间就消失了:

当设备处于横向模式并且我开始搜索时,表格视图被切断:

然后,如果我旋转到纵向,则表格视图非常适合:

额外的空间高度与状态栏高度完全相同。所以我想知道这里有什么与状态栏有关的东西。

如果我将搜索栏指定为表格视图的tableHeaderView,则不会发生这种情况。仅当搜索栏位于导航标题中时才会发生。有人知道为什么吗?

【问题讨论】:

标签: ios uisearchbar uisearchcontroller


【解决方案1】:

显然automaticallyAdjustsScrollViewInsets 已被弃用。

'automaticallyAdjustsScrollViewInsets' 在 iOS 11.0 中已弃用:改用 UIScrollView 的 contentInsetAdjustmentBehavior

所以我建议这样做:

searchResultsController.tableView.contentInsetAdjustmentBehavior = .never

它对我来说就像一个魅力。一些有用的文档:

contentInsetAdjustmentBehavior

UIScrollViewContentInsetAdjustmentBehavior

【讨论】:

  • 我也面临同样的问题,但设置 tableView.contentInsetAdjustmentBehavior = .never 不起作用:(
  • 如果您使用 UITableViewController 作为 searchResultUpdater 而不是其中包含 UITableViewUIViewController,它就像一个魅力。
  • UIViewController中TableView有什么解决办法吗?
  • @AlizainPrasla 你应该使用这样的东西然后 tableView.contentInsetAdjustmentBehavior = .never tableView.contentInset = UIEdgeInsets(top: 120, left: 0, bottom: 0, right: 0)
【解决方案2】:

写在viewDidLoad

self.automaticallyAdjustsScrollViewInsets = false

【讨论】:

  • 默认是true,我没有碰这个。我想使用透明导航栏和标签栏,所以我必须保留这个true
【解决方案3】:

这个问题只能在iOS 10出现。把这个写在viewDidLoad中

if #available(iOS 10.0, *) {
automaticallyAdjustsScrollViewInsets = false
tableView.contentInset = UIEdgeInsetsMake(64, 0, 44, 0)}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-08
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多