Desgard_Duan
Desgard_Duan

Reputation: 709

'cancel' button of searchbar disappear in iPad

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.

Upvotes: 1

Views: 1819

Answers (1)

Desgard_Duan
Desgard_Duan

Reputation: 709

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

Upvotes: 1

Related Questions