活动时,UISearchBar与UITableView内容重叠

时间:2016-05-02 22:58:27

标签: ios swift uitableview uinavigationbar uisearchcontroller

我有一个带有表视图和UISearchController的视图控制器。运行应用程序时,我发现搜索栏在活动时与内容重叠。当搜索栏处于活动状态时,我需要调整什么才能使内容不重叠?

普通视图:

enter image description here

搜索栏处于有效状态:

enter image description here

查看控制器设置:

enter image description here

5 个答案:

答案 0 :(得分:5)

问题是因为你有automaticallyAdjustsScrollViewInsets = true

请取消选中enter image description here

会有所帮助:)

答案 1 :(得分:1)

您可以这种方式使用' UISearchController ':

_searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
self.searchController.searchResultsUpdater = self;
[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar;

// we want to be the delegate for our filtered table so didSelectRowAtIndexPath is called for both tables
self.resultsTableController.tableView.delegate = self;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES
self.searchController.searchBar.delegate = self; // so we can monitor text changes + others

// Search is now just presenting a view controller. As such, normal view controller
// presentation semantics apply. Namely that presentation will walk up the view controller
// hierarchy until it finds the root view controller or one that defines a presentation context.
//
self.definesPresentationContext = YES;  // know where you want UISearchController to be displayed

您可以使用此工作Apple Reference Sample code获取更多详细信息。

答案 2 :(得分:1)

将此内容写在您的viewDid()中。

来源:apple

if #available(iOS 11.0, *) {
    // For iOS 11 and later, place the search bar in the navigation bar.
    navigationItem.searchController = searchController

    // Make the search bar always visible.
    navigationItem.hidesSearchBarWhenScrolling = false
} else {
    // For iOS 10 and earlier, place the search controller's search bar in the table view's header.
    tableView.tableHeaderView = searchController.searchBar
}

答案 3 :(得分:0)

尝试将SearchBar放入TableView的标题中。

答案 4 :(得分:0)

这与在iOS 11上滚动时取消隐藏搜索栏有关。

if #available(iOS 11.0, *) {
  navigationItem.searchController = searchController
  navigationItem.hidesSearchBarWhenScrolling = false
} else {
  // Fallback on earlier versions
  tableView?.tableHeaderView = searchController.searchBar
}