swiftipaduisearchbar

Searchbar's cancel button not visible on iPads


First I want to say that I didn't find any good answer about the topic, so I ask the following question.

I created searchbar using storyboard. The problem is that on the search button click, it shows the searchbar and on cancel it hides the searchbar, and it is working properly on iPhones. But for some weird reason the cancel button is not visible on iPads. What can cause this?

This is how I create the searchbar:

//Create searchbar
    func createSearchBar(){
        
        searchBar.showsCancelButton = true
        searchBar.tintColor = UIColor(red:0.184, green:0.996, blue:0.855, alpha:1.00)
        searchBar.placeholder = "Search brands"
        searchBar.delegate = self
        
       
        
        searchBar.hidden =  false
        searchBar.alpha = 0
        
        navigationItem.titleView = searchBar
        navigationItem.setLeftBarButtonItem(menuButton, animated: true)
        navigationItem.setRightBarButtonItem(searchButtton, animated: true)
        
        
        UIView.animateWithDuration(0.5, animations: {
            self.searchBar.alpha = 1
            }, completion: { finished in
                self.searchBar.becomeFirstResponder()
        })
        
        
    }

Solution

  • Faced the same one of my project. I've posted on apple forum and lot of developers commented as a xcode bug. So I added the cancel button manually for ipad views

     func configureCancelBarButton() {
        let cancelButton = UIBarButtonItem()
        cancelButton.image = UIImage(named: "cancelButton")
        cancelButton.action = #selector(viewController.ipadCancelButton(_:))
        cancelButton.target = self
        self.navigationItem.setRightBarButtonItem(cancelButton, animated: true)
    }
    

    And I previously posted an answer about this question. Check that too. :)