我在导航栏中有搜索图标,在TableViewHeader中有UISearchBar。 我想隐藏UISearchBar,但在以下时间显示和隐藏:
UISearchbar show :点击搜索图标,然后取消导航并显示它。
UISearchBar隐藏 strong>:按取消隐藏搜索并显示返回的导航。
请帮助我!!
感谢。
这是我到目前为止所做的。
- (void) viewDidload{
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
self.searchBar.placeholder = @"Search";
self.searchBar.delegate = self;
[self.searchBar sizeToFit];
self.strongSearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.delegate = self;
self.tableView.tableHeaderView = self.searchBar;
_btnSearch = [UIButton buttonWithType:UIButtonTypeCustom];
_btnSearch.frame = CGRectMake(0, 0, 22, 22);
[_btnSearch setImage:[UIImage imageNamed:@"search_btn.png"] forState:UIControlStateNormal];
[_btnSearch addTarget:self action:@selector(showSearch:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithCustomView:_btnSearch];
self.navigationItem.rightBarButtonItems = _btnSearch;
}
点击iCon搜索
时,这是我的动作- (IBAction)showSearch:(id)sender{
[self.searchBar becomeFirstResponder];
}
我不想将我的搜索放在tableView中,当点击搜索图标时我怎么能显示UISearchBar?
另一个问题:当我调用[self.searchBar becomeFirstResponder]时;它不好用。取消按钮根本不起作用。
请帮助我。
答案 0 :(得分:4)
将searchBar放入navigationBar的中心:
self.navigationItem.titleView = self.searchBar;
将searchBar放在navigationBar的左/右侧:
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:self.searchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;
从iOS 7开始,UISearchDisplayController默认支持此功能。设置UISearchDisplayController
displaysSearchBarInNavigationBar = YES