如何修复表视图的搜索栏与状态栏重叠

如何修复表视图的搜索栏与状态栏重叠

问题描述:

我在导航控制器中有一个UITableViewController,带有一个搜索栏。这是我在viewDidLoad中添加搜索栏的方式:

I have a UITableViewController inside a navigation controller, with a search bar. This is how I add the search bar in viewDidLoad:

let resultsController = SearchTableViewController()
resultsController.people = people
searchController = UISearchController(searchResultsController: resultsController)
let searchBar = searchController.searchBar
searchBar.placeholder = "Search a person"
searchBar.sizeToFit()
tableView.tableHeaderView = searchBar
searchController.searchResultsUpdater = resultsController

结果如下:

我尝试在故事板中编辑表格视图以添加约束以使其从顶视图的边距更远,但我无法添加约束,可能是因为表视图位于UITableViewController中。

I tried editing the table view in the storyboard to add a constraint to make it further from the top view's margins, but I can't add contraints, probably because the table view is inside a UITableViewController.

我认为你需要这段代码。

I think you need this code.

在你的 viewDidLoad 方法添加此代码:

In your viewDidLoad method add this code:

self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)

您的tableview将是这样的:

And your tableview will be something like this:

编辑:

您可以使用以下代码强制滚动表格:

You can forcely scroll table with this code:

tableView.scrollToRowAtIndexPath( NSIndexPath(index: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)