我有UISearchBar
进行了一些自定义,我创建了一个像这样的UISearchDisplayController
self.searchController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
我希望搜索栏显示在导航栏上,所以我也设置了
self.searchDisplayController.displaysSearchBarInNavigationBar = true;
现在搜索栏显示在导航栏中,但我只想在点击导航栏按钮项时显示UISearchDisplayController
的搜索栏。我希望有这样的行为:
我试图隐藏/取消隐藏它:
self.searchDisplayController.searchBar.hidden = YES;
但代码似乎不起作用。我花了很多时间搜索解决方案以获得我想要的行为,但仍然没有运气。感谢。
答案 0 :(得分:0)
试试这个:
CGRect searchFrame = self.searchDisplayController.searchBar.frame;
searchFrame.size.height = 0;
self.searchDisplayController.searchBar.frame = searchFrame;
self.searchDisplayController.searchBar.hidden = YES;
编辑:我刚试过下面的代码就行了。看看这对你有帮助!
- (void)viewDidLoad {
[super viewDidLoad];
[self.searchDisplayController.searchContentsController.navigationController setNavigationBarHidden:YES animated:YES];
[self performSelector:@selector(test) withObject:nil afterDelay:2.0];
}
- (void)test {
[self.searchDisplayController.searchContentsController.navigationController setNavigationBarHidden:NO animated:YES];
}
答案 1 :(得分:0)
也许这是一个合适的解决方案?
要完成任务1,请在您的TVC生命周期方法viewDidLoad
中插入一个非动画滚动条,将搜索栏放在导航栏下方...
- (void)viewDidLoad {
[super viewDidLoad];
// Scroll off screen the search bar (44 points)
[[self tableView] setContentOffset:CGPointMake(0, 44)];
// other code for method
}
要完成任务2,请在您的TVC中创建与UIBarButtonItem
相关联的自定义操作方法,该方法可有效隐藏导航栏,同时显示搜索栏...
- (IBAction)hideNavBar:(UIBarButtonItem *)sender {
[[self navigationController] setNavigationBarHidden:YES animated:YES]
}
要完成任务3,请在您的TVC中使用UISearchDisplayController
委托方法有效地显示导航栏,同时隐藏搜索栏...
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[[self navigationController] setNavigationBarHidden:NO animated:YES];
[[self tableView] setContentOffset:CGPointMake(0, 44)];
// You might also like to...
[self setSearchBarText:nil]; // if you are using a property to hold the search bar text
[self setSearchResults:nil]; // if you are using a property to hold the search results
}