UISearchBar Cancel Button

前端 未结 6 2789
别那么骄傲
别那么骄傲 2021-02-20 03:35

Using a UISearchBar with showCancelButton=YES on iOS 5. Would like the cancel button to stay enabled when the keyboard drops down. Using the following code seems not to work:<

6条回答
  •  梦如初夏
    2021-02-20 04:42

    Set your searchbar delegate and than put this code.

    - (void) searchBarSearchButtonClicked:(UISearchBar*) theSearchBar
      {
         [theSearchBar resignFirstResponder];
         [theSearchBar setShowsCancelButton:NO animated:YES];
      }
     - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
      {
         [searchBar setShowsCancelButton:YES animated:YES];
      }
      - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
      {
         [searchBar resignFirstResponder];
         [searchBar setShowsCancelButton:NO animated:YES];
      }
    

    Swift 3.0

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
         searchBar.resignFirstResponder()
         searchBar.setShowsCancelButton(false, animated: true)
    }
    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
         searchBar.setShowsCancelButton(true, animated: true)
    }
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
         searchBar.resignFirstResponder()
         searchBar.setShowsCancelButton(false, animated: true)
    }
    

提交回复
热议问题