我在导航的右侧有一个按钮搜索。这是我的代码:
UIButton *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 = searchItem ;
这是它的外观。
我想在单击搜索按钮后显示搜索栏,在单击“取消”后将其关闭,然后向后显示导航栏,但是我不知道如何编码。
- (IBAction)showSearch:(id)sender{
???
}
这就是我想要的。
请帮助或提供一些示例代码。我真的很需要
感谢阅读。
UISearchBar *mySearchBar
向您的viewController添加一个属性为
@property(nonatomic, retain) UISearchBar *mySearchBar;
顺应UISearchBarDelegate
作为
@interface HomeViewController () <UISearchBarDelegate>
...
...
@end
然后将showSearch
方法实现为
-(void)showSearch:(id)sender {
if(!mySearchBar) {
mySearchBar = [[UISearchBar alloc] init];
[mySearchBar setFrame:CGRectMake(0, 0, self.view.frame.size.width, 55)];
[mySearchBar setShowsCancelButton:YES animated:YES];
[self.view addSubview: mySearchBar];
mySearchBar.delegate = self;
self.navigationController.navigationBarHidden = YES;
}else {
searchBar.alpha = 1.0;
self.navigationController.navigationBarHidden = YES;
}
然后将搜索栏委托方法实现为:
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
self.navigationController.navigationBarHidden = NO;
[mySearchBar setAlpha:0.0];
}
希望您通过这样做有个主意。您也可以将其直接添加到导航控制器本身,如果需要,然后与“显示/隐藏”一起玩searchBar
。
您可以将其添加到导航控制器中,只需初始化mysearchBar
&将其添加navigationBar
为:
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:mySearchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句