how to show cancel button of searchbar?

后端 未结 6 1352
深忆病人
深忆病人 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:55

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

    Swift

    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)
    }
    

提交回复
热议问题