Sheik_101
Sheik_101

Reputation: 770

Cancel button is not shown in UISearchBar

I have UICollectionView. On clicking search button in UINavigationBar, I am adding the UISearchController's searchbar as titleview for UINavigationItem. For iPhone it is working properly. For iPad the cancel button is not shown. The Searchbar alone takes the entire width.

enter image description here

Can anyone help me out on this?. Thanks in advance.

Upvotes: 24

Views: 14480

Answers (7)

Nostradamus
Nostradamus

Reputation: 1557

For iOS 13 built with Xcode 11, I'm needing to set manually set the display value on the cancel button, depending on whether the search controller is visible

Upvotes: 0

Muhammad Usman Aleem
Muhammad Usman Aleem

Reputation: 197

As per apple documentation setShowsCancelButton

Cancel buttons are not displayed for apps running on iPad, even when you specify YES for the showsCancelButton parameter.

I am not sure about the alternate but this is what apple provides us.

enter image description here

Upvotes: 4

Sagar Daundkar
Sagar Daundkar

Reputation: 320

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

Upvotes: 5

Mudith Chathuranga Silva
Mudith Chathuranga Silva

Reputation: 7434

Swift version :-

I tried the @Nikita Khandelwal method, but still it doesn't fit for ipad view. Here is the swift code, which was given as 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 cancel button correctly and fit for the view :-

  1. Set search bar as the Navigation bar title view :-

    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
    
  2. Drag and drop Bar button to the right side of the view controller & name it as Cancel.

  3. Then connect that button to this function :-

    @IBAction func iPadCancelButton(sender: AnyObject) {
           UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)
          self.dismissViewControllerAnimated(true, completion: nil)
    }
    

Upvotes: 1

Tal Haham
Tal Haham

Reputation: 1628

I had the same problem, on iPhone the search cancel was shown well, but on iPad it didn't.

The workaround of wrapping the UISearchBar in another UIView didn't work well for me since it had different appearance and wrong width on rotation.

My solution is a simple one - use search WITHOUT cancel, and add cancel as a UIBarButtonItem.

Upvotes: 8

Nikita Khandelwal
Nikita Khandelwal

Reputation: 1741

iOS7 does not show the cancel button when added to a navigation bar.You can put searchbar 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;

Upvotes: 18

Kaey
Kaey

Reputation: 4645

Try this. Add a checkmark for shows cancel button.

enter image description here

Upvotes: 2

Related Questions