UISearchController searchBar showsCancelButton not

2020-06-08 13:54发布

I've added a UISearchController to my application and set it's searchBar to the titleView of my navigationItem.

This works but I am seeing the cancel button despite having set showsCancelButton to false.

    searchController = UISearchController(searchResultsController: searchResultsController)
    searchController.searchResultsUpdater = searchResultsUpdater


    // Configure the searchBar
    searchController.searchBar.placeholder = "Find Friends..."
    searchController.searchBar.sizeToFit()
    searchController.searchBar.showsCancelButton = false

    self.definesPresentationContext = true

    navigationItem.titleView = searchController.searchBar

enter image description here

标签: ios xcode swift
12条回答
Animai°情兽
2楼-- · 2020-06-08 14:01

my solution was to set the attribute every time anew when I used the searchcontroller respectively its searchbar. I initialized the searchcontroller lazily without setting the attribute and then did

searchController.searchBar.showsCancelButton = false

every time before search began. You could do this in the UISearchControllerDelegate methods i.e...

查看更多
Explosion°爆炸
3楼-- · 2020-06-08 14:03

I had to correct by putting in a little hack...

Setting the alpha to 0.0 on viewDidLoad because he screen will flash.

Before you ask...willPresentSearchController will not work.

extension GDSearchTableViewController: UISearchControllerDelegate {
    func didPresentSearchController(searchController: UISearchController) {
        searchController.searchBar.setShowsCancelButton(false, animated: false)
        searchController.searchBar.becomeFirstResponder()
        UIView.animateWithDuration(0.1) { () -> Void in
            self.view.alpha = 1.0
            searchController.searchBar.alpha = 1.0
        }
    }
}
查看更多
ら.Afraid
4楼-- · 2020-06-08 14:06

I agree, it seems like a bug. The problem is that the searchController keeps resetting the showsCancelButton property of the searchBar. I found a solution that involves:

  1. subclassing UISearchBar to ignore setShowsCancelButton.
  2. to make the searchController use that subclass, you have to subclass UISearchController.
  3. And then you find that the searchBar is not triggering the search controller's delegate methods, so you have to trigger them separately...

Convoluted, but it seems to do the trick. You can find the full answer here.

查看更多
姐就是有狂的资本
5楼-- · 2020-06-08 14:06

This appears to be a bug in iOS. The same behavior I've described can be seen in the example project supplied by Apple

https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html

The documentation states that the default for this is NO but this doesn't seem to be the case. Setting showsCancelButton to NO seems to have no effect.

I have filed a radar for this and am waiting to hear back.

查看更多
家丑人穷心不美
6楼-- · 2020-06-08 14:07

It may be helpful to note that this has changed on iOS 13 and quote Apple's documentation on showsCancelButton, currently only available on UISearchBar.h and not on developer.apple.com

/* New behavior on iOS 13.
 If the search bar is owned by a UISearchController, then using the setter
 for this property (as well as -setShowsCancelButton:animated:) will implicitly
 set the UISearchController's automaticallyShowsCancelButton property to NO.
 */

automaticallyShowsCancelButton has been introduced on iOS 13.0 and should clarify what @pbasdf had already pointed out in his answer: that the buggy behavior is something intrinsic to UISearchController.

查看更多
我命由我不由天
7楼-- · 2020-06-08 14:09

I would also add

searchController.hidesNavigationBarDuringPresentation = false
searchController.delegate = self
searchController.searchBar.delegate = self

See if assigning those delegates will help.

查看更多
登录 后发表回答