Cancel button is not shown in UISearchBar
Asked Answered
L

7

24

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.

Led answered 27/5, 2015 at 6:15 Comment(5)
Are you using different storyboards or xib for iPhone and iPad?Artema
No. I am not using storyboards or xib.Led
Try to set searchBar.showsCancelButton = YES;Artema
possible duplicate of iOS7 when UIsearchbar added in UINavigationBar not showing cancel buttonArtema
The documentation clearly states: "The value of this property is ignored, and no cancel button is displayed, for apps running on iPad". I don't think this is appropriate, especially for slide over / split view scenarios. So I filed a radar: openradar.appspot.com/37498710Bresnahan
N
18

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;
Nofretete answered 27/5, 2015 at 6:24 Comment(1)
Good pick from : #18986907Artema
B
8

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.

Bealle answered 8/11, 2015 at 7:14 Comment(1)
This seems like the correct answer to me. The cancel button isn't supported on iPad, so just add it manually as a UIBarButtonItem.Wetnurse
J
5
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)
        }
    }
Job answered 5/12, 2017 at 6:34 Comment(0)
P
4

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

Personnel answered 1/1, 2019 at 10:37 Comment(0)
I
2

Try this. Add a checkmark for shows cancel button.

enter image description here

Ihab answered 27/5, 2015 at 6:21 Comment(1)
the OP mentioned that it works in iPhone but not iPadCiprian
M
1

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)
    }
    
Messaline answered 1/12, 2015 at 11:23 Comment(0)
C
0

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

Capone answered 16/9, 2019 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.