1

My app needs support universal devices.

And in that, there is a view controller which have UISearchController. It can show normally in iPhone device, but in iPad, the 'cancel' button disappear.

iPhone

iPad

It is the relevant code about searchbar in the view controller, following.

func configureSearchController() {
    self.collectionView!.updateConstraints()
    resultsTableController = SearchBookResultTableViewController()
    resultsTableController.tableView.delegate = self
    searchController = UISearchController(searchResultsController: resultsTableController)
    searchController.searchResultsUpdater = self
    navigationItem.titleView = searchController.searchBar
    searchController.searchBar.sizeToFit()
    searchController.dimsBackgroundDuringPresentation = false
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.searchBar.delegate = self
    searchController.searchBar.searchBarStyle = .Minimal
    searchController.searchBar.placeholder = "书籍、作者、出版社"
    // stress
    searchController.searchBar.showsCancelButton = true
    definesPresentationContext = true
}

I want to know the reason of the problem, and in which way I can solve it.

Seek help! Thx.

Desgard_Duan
  • 659
  • 1
  • 6
  • 12
  • This is a guess - try moving the .showsCancelButton before the .sizeToFit() – Mike Taverne Apr 09 '16 at 02:27
  • @MikeTaverne I try your way, and the problem still exits. The `showsCancelButton` property is `true`, which is default. I only want to stress the property is true, so I write it. *>.<* – Desgard_Duan Apr 09 '16 at 02:37
  • Given that there is the "keyboard dismiss" button on the iPad keyboard, the cancel button isn't really necessary. – tktsubota Apr 09 '16 at 04:31

1 Answers1

1

I found the same situation in iOS 7 in this link. And I use the same way to solve my problem. But I don't know why the way, create a new UIView, can solve it? What's the different between them?

self.collectionView!.updateConstraints()
resultsTableController = SearchBookResultTableViewController()
resultsTableController.tableView.delegate = self
searchController = UISearchController(searchResultsController: resultsTableController)
searchController.searchResultsUpdater = self
searchController.searchBar.sizeToFit()
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.delegate = self
searchController.searchBar.searchBarStyle = .Minimal
searchController.searchBar.placeholder = "书籍、作者、出版社"

let SearchView: UIView = UIView(frame: searchController.searchBar.bounds)
SearchView.addSubview(searchController.searchBar)

navigationItem.titleView = SearchView
Community
  • 1
  • 1
Desgard_Duan
  • 659
  • 1
  • 6
  • 12