当UISearchBar处于激活状态时,它会与UITableView的内容重叠。

8
我是一名有用的助手,可以为您进行文本翻译。以下是需要翻译的内容:

我有一个视图控制器,其中包含一个表格视图和一个 UISearchController。运行应用程序时,我发现搜索栏在激活状态下会覆盖内容。我需要调整什么才能使内容在搜索栏激活时不被覆盖?

正常视图:

enter image description here

搜索栏已激活:

enter image description here

查看控制器设置:

enter image description here


你是否执行了 edgesForExtendedLayout = .NoneautomaticallyAdjustsScrollViewInsets = false - Ozgur Vatansever
@ozgur 没有。我已经包含了设置的截图。 - Boon
你的搜索栏在一个图片中位于导航栏下方,在另一个图片中则是它的子视图,请解释它是如何呈现的。 - Wain
为什么不能使用自动布局? - rakeshNS
@rakeshNS 我正在使用自动布局。 - Boon
我仍然面临着相同的问题..... - Mihir Mehta
5个回答

5
问题是因为你设置了 automaticallyAdjustsScrollViewInsets = true,请取消勾选这个选项 enter image description here,这样会有所帮助 :)

谢谢 - 这个加上布局约束的更改就解决了。 - Boon
在表格视图中,通常应该关闭“调整滚动视图插入”吗? - Boon
即使我在使用自动布局时,我也更喜欢将其关闭。 - Prashant Tukadiya
谢谢 - 明天会授予奖励。 - Boon
我仍然面临着同样的问题......我的部分标题与tableHeaderView重叠。 - Mihir Mehta
尝试在顶部栏下取消选中@mihirmehta - Prashant Tukadiya

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

你可以使用这个苹果参考示例代码来获得更多细节。

你应该将“SearchBar”放置在表格的头视图中。 - itechnician

1

请在您的viewDid()中编写此代码。

来源: 苹果

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
}

0

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接