【发布时间】:2020-04-16 07:51:52
【问题描述】:
我的应用程序中有UISearchController:
-(void)setSearchBar {
self.resultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
[self.resultsController.tableView registerClass:UITableViewCell.self forCellReuseIdentifier:@"SearchTitleCell"];
self.resultsController.tableView.dataSource = self;
self.resultsController.tableView.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsController];
[self.searchController setSearchResultsUpdater:self];
[self.searchController setDelegate:self];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.1")) {
self.searchController.obscuresBackgroundDuringPresentation = NO;
}
self.extendedLayoutIncludesOpaqueBars = YES;
self.navigationItem.searchController = self.searchController;
self.searchController.searchBar.delegate = self;
self.definesPresentationContext = true;
self.searchController.hidesNavigationBarDuringPresentation = false;
self.searchController.searchBar.showsScopeBar = NO;
self.searchController.searchBar.scopeButtonTitles = @[@"One", @"two", @"three"];
}
我想在点击UISearchBar栏时添加Scopebar,并在退出UISearchBar时隐藏它,所以我使用以下代码:
- (void)willPresentSearchController:(UISearchController *)searchController {
self.searchController.searchBar.showsScopeBar = YES;
[self.searchController.searchBar sizeToFit];
}
- (void)willDismissSearchController:(UISearchController *)searchController {
self.searchController.searchBar.showsScopeBar = NO;
[self.searchController.searchBar sizeToFit];
}
问题是当我关闭UISearchBar(通过单击取消按钮)时,我仍然可以看到范围空白。
知道如何解决这个问题吗?
【问题讨论】:
-
你能解释更多你真正想做的事情吗?你想在取消按钮上隐藏搜索栏,或者你想隐藏搜索栏图像。
-
@VipinPareek 我想隐藏搜索栏范围栏
-
创建一个搜索栏的出口,然后当你点击取消按钮时,隐藏搜索栏的代码就像 yoursearchbar_object_name.ishidden = true 一样,当你想再次显示搜索栏时你写类似 yoursearchbar_object_name.ishidden = false 的代码
-
@VipinPareek 搜索栏范围被隐藏,但留下一个空白空间
-
然后在隐藏搜索栏时添加搜索栏高度约束的出口,然后您将编写一行代码,也类似于 search_bar_height_constraints_object_name.constant = 0 // 这一行写在您隐藏搜索栏时你显示搜索栏,然后写 search_bar_height_constraints_object_name.constant = 你的实际搜索栏高度,比如 40。你的空间就消失了。谢谢
标签: ios objective-c swift iphone uisearchbar