how to show cancel button of searchbar?

后端 未结 6 1353
深忆病人
深忆病人 2021-02-19 06:11

I want to hide the Navigation Bar of my page when the user start editing on the searchbar, I also want to show a cancel button.
It is done but my cancel button is not access

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-19 06:43

    You need to use UISearchDisplayController, that will hide navigation bar automatically for you. Here is the code,

    -(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]];  
    }
    

    You will have to implement protocols UISearchBarDelegate, UISearchDisplayDelegate, UITableViewDelegate, UITableViewDataSource in .h file.

    also _searchBar & searchDisplayController are variables defined in .h file.

提交回复
热议问题