当用户在搜索栏上开始编辑时,我想要隐藏页面的导航栏,我还想显示取消按钮。
完成后,当我调出UISearchBar时,我的取消按钮无法访问
感谢All。
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
self.navigationController.navigationBar.hidden=TRUE;
CGRect r=self.view.frame;
r.origin.y=-44;
r.size.height+=44;
self.view.frame=r;
searchBar.showsCancelButton=TRUE;
}
答案 0 :(得分:34)
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
self.navigationController.navigationBar.hidden = TRUE;
CGRect r = self.view.frame;
r.origin.y = -44;
r.size.height += 44;
self.view.frame = r;
[searchBar setShowsCancelButton:YES animated:YES];
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
}
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
self.navigationController.navigationBar.hidden = true
var r = self.view.frame
r.origin.y = -44
r.size.height += 44
self.view.frame = r
searchBar.setShowsCancelButton(true, animated: true)
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchBar.setShowsCancelButton(false, animated: true)
}
答案 1 :(得分:14)
使用此代码在SearchBar
用户将在SearchBar中开始编辑,显示取消按钮。
只有正确设置了SearchBar
的代理人才能访问以下方法。
-(void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
//This'll Show The cancelButton with Animation
[searchBar setShowsCancelButton:YES animated:YES];
//remaining Code'll go here
}
隐藏CancelButton
用户点击取消按钮
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
//This'll Hide The cancelButton with Animation
[searchBar setShowsCancelButton:NO animated:YES];
//remaining Code'll go here
}
答案 2 :(得分:2)
使用此代码
searchBar.showsCancelButton=YES;
答案 3 :(得分:1)
使用此代码显示cancel
按钮。当您在searchBar
中输入文字时,它会显示按钮。
-(void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:YES];
}
答案 4 :(得分:1)
快捷键4:
下面两行代码为我显示了取消按钮:
searchBar.setShowsCancelButton(true, animated: true)
searchBar.showsCancelButton = true
在searchBarTextDidBeginEditing()
方法内调用将在单击搜索栏后显示取消按钮。
答案 5 :(得分:0)
您需要使用UISearchDisplayController,它会自动隐藏导航栏。 这是代码,
-(void) createSearchBar
{
_searchBar = [[SearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
_searchBar.delegate = self;
[self.view addSubview:_searchBar];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
searchDisplayController.searchResultsTableView.sectionHeaderHeight = 10;
searchDisplayController.searchResultsTableView.sectionFooterHeight = 0;
[searchDisplayController.searchResultsTableView setSeparatorColor:[Color whiteColor]];
}
您必须在.h文件中实现协议UISearchBarDelegate,UISearchDisplayDelegate,UITableViewDelegate,UITableViewDataSource。
也是_searchBar& searchDisplayController是.h文件中定义的变量。