如何显示搜索栏的取消按钮?

时间:2012-10-23 10:42:46

标签: iphone uinavigationbar uisearchbar

当用户在搜索栏上开始编辑时,我想要隐藏页面的导航栏,我还想显示取消按钮。
完成后,当我调出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;

}

6 个答案:

答案 0 :(得分:34)

目标C

- (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文件中定义的变量。