UISearchController searchBar 在第一次点击时消失 [英] UISearchController searchBar disappears on first click
问题描述
我在 TableView 中实现了一个 UISearchController,由导航控制器推送.
I have implemented a UISearchController in a TableView, pushed by a Navigation Controller.
首先我的问题是,每当我点击 SearchBar 时,它就会消失.当我输入一些文本时它起作用,但它保持完全空白.然后我设法使用此代码半解决了这个问题:
First my problem was that whenever I click on the SearchBar, it disappears. It works when I enter some text, but it stays completely blank. Then I managed to semi solve the issue using this code:
- (void)searchForText:(NSString*)searchText
{
[self.view addSubview:villeSearchController.searchBar];
}
这是半工程,因为现在,当我点击搜索栏时,它会变黑,但如果我输入一个字符,它会再次出现,然后它会一直留在那里,无论如何.直到我取消搜索,然后再次单击它,在这种情况下它才会消失.我已经做了一些测试,并且在第一次点击时调用了这个方法 (searchForText),所以这不是原因.
Which semi-works because now, when I click on the search bar, it blanks out, but if I enter one character, it appears again, and then it stays there, no matter what. Until I cancel the search, and click on it again, in that case it blanks out. I have made some tests and this method (searchForText) is called on the very first click, so that isn't the reason.
有谁知道我如何解决这个问题并使搜索栏在第一次点击时出现?
Does anyone know how I can solve this issue and make the searchbar appear from the very first click?
这是我初始化 SearchController 的方式:
This is how I initialize the SearchController:
villeSearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
villeSearchController.searchResultsUpdater = self;
villeSearchController.dimsBackgroundDuringPresentation = NO;
villeSearchController.searchBar.delegate = self;
villeTableView.tableHeaderView = villeSearchController.searchBar;
villeSearchController.searchBar.scopeButtonTitles = @[];
self.definesPresentationContext = YES;
[villeSearchController.searchBar sizeToFit];
推荐答案
尝试检查 navigationBar.translucent
属性 - 当 UISearchController
将呈现searchBar
否则将是 UI 错误.
Try to check the navigationBar.translucent
property - it should be YES when UISearchController
will present the searchBar
or else will be UI bugs.
来自@SiavA 的更新
更好的解决方案是使用 UIViewController
的 extendedLayoutIncludesOpaqueBars
属性.如果您使用不透明的导航栏,只需将其设置为控制器的 true
将显示 UISearchController
(不适用于 navigationController
).
The better solution is use the extendedLayoutIncludesOpaqueBars
property of the UIViewController
. If you using the opaque navigation bar just set it in the true
for controller which will be show UISearchController
(not for navigationController
).
例如
- (void)viewDidLoad {
[super viewDidLoad];
self.extendedLayoutIncludesOpaqueBars = !self.navigationController.navigationBar.translucent;
}
这篇关于UISearchController searchBar 在第一次点击时消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!