Cancel button does not appear in UISearchBar

I have a UICollectionView . When I click the search button in the UINavigationBar I add the UISearchController searchbar as the title for the UINavigationItem . For iPhone, it works correctly. For iPad, the cancel button is not displayed. Only the search bar accepts the entire width.

enter image description here

Can someone help me with this? Thanks in advance.

+15
ios objective-c uisearchbar uisearchcontroller
May 27 '15 at 6:15
source share
5 answers

iOS7 does not display a cancel button when added to the navigation bar. You can put the search bar in another view like this.

 UISearchBar *searchBar = [UISearchBar new]; searchBar.showsCancelButton = YES; [searchBar sizeToFit]; UIView *viewForSearchBar = [[UIView alloc]initWithFrame:searchBar.bounds]; [viewForSearchBar addSubview:searchBar]; self.navigationItem.titleView = viewForSearchBar; 
+16
May 27 '15 at 6:24
source share
— -

I had the same problem, on the iPhone the cancellation of the search was shown well, but on the iPad this did not happen.

The workaround for wrapping a UISearchBar in another UIView did not work for me, since it had a different appearance and the wrong width when rotating.

My solution is simple: use the search WITHOUT cancel and add the cancel as UIBarButtonItem .

+6
Nov 08 '15 at 7:14
source share

Try it. Add a checkmark to the cancel cancel button.

enter image description here

+2
May 27 '15 at 6:21
source share

Quick version: -

I tried @Nikita Khandelwal's method, but still it is not suitable for watching ipad. Here is the quick code that was set as the corrected answer: -

 let searchBar: UISearchBar = UISearchBar() searchBar.showCancelButton = true searchBar.placeholder = "Search Your Job Title" searchBar.fitToSize() searchBar.delegate = self //do not need if you delegate searchBar let viewForSearchBar: UIView = UIView(frame: searchBar.bounds) viewForSearchBar.addSubview(searchBar) self.navigationItem.titleView = viewForSearchBar 

********* But there is another way to set the cancel button correctly and customize for presentation: -

  • Set the search bar as the title of the navigation bar: -

     let searchBar: UISearchBar = UISearchBar() searchBar.showCancelButton = true searchBar.placeholder = "Search Your Job Title" searchBar.delegate = self //do not need if you delegate searchBar self.navigationItem.titleView = searchBar 
  • Drag the Bar button to the right side of the view controller and name it Cancel.

  • Then connect this button to this function: -

     @IBAction func iPadCancelButton(sender: AnyObject) { UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil) self.dismissViewControllerAnimated(true, completion: nil) } 
0
Dec 01 '15 at 11:23
source share
 Added rightBarButtonItem with selector will work fine for me. And adding searchBar inside view before setting to navigation title view was not showing properly. Code:- self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.dismissView)) func dismissView() { if self.controller?.navigationController?.popViewController(animated: true) == nil { self.controller?.dismiss(animated: true, completion: nil) } } 
0
Dec 05 '17 at 6:34 on
source share



All Articles