Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 5 & iOS 13 UISearchController wrong presenting and dismissing behaviour

While i was updating my project to iOS 13 i faced with unusual issue. I have logic for showing and handling some UISearchController actions (code below), all parts were working perfectly in iOS 11 & 12.

My task was to add search button at navigation bar to show UISearchController after button action.

But in iOS 13, i got 2 issues:

Code that invokes from button action

func showSearch() {
        let searchResultsController = LUSearchResultsViewController()...
        let searchController = UISearchController(searchResultsController: searchResultsController)
        searchController.delegate = self
        searchController.searchResultsUpdater = searchResultsController
        navigationItem.searchController = searchController
        definesPresentationContext = true
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            searchController.isActive = true
        }
}

Only one solution helps me to show search controller without any diffs from previous iOS versions.

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            searchController.isActive = true
}

Code that works on iOS 13, on iOS 11 and 12 code works without asyncAfter

Code that helps me to hide UISearchController and set navigation bar initial state in iOS 11 and 12, and not in 13. After tapping cancel on SearchBar, UISearchController begins to call delegate methods.

  //MARK: UISearchControllerDelegate


    public func willDismissSearchController(_ searchController: UISearchController) {
         self.navigationItem.searchController = nil
    }

BUT

After setting navigationItem.searchController = nil didPresentSearchController calls twice and not only UISearchController dismissed, but whole UIViewController hierarchy right to UIWindow. Or if i add asyncAfter here i get normal dismiss with expanded nav bar height, that i don't need.

SO

  1. I think that asyncAfter in a dog-nail, how would you solve this problem?
  2. How can i dismiss UISearchController correctly?
like image 959
Wart Deider Avatar asked Sep 24 '19 12:09

Wart Deider


People also ask

Is the Acer Swift 5 GOOD?

Impressive battery life, a svelte design, a tactile keyboard, and zippy performance scores are the most important features I look for while reviewing laptops, and the Swift 5 met — and even surpassed — my expectations. The refreshed Acer Swift 5 comes with a new 12th Gen Intel Core i7 CPU that outperforms its rivals.

Is Swift 5 touch screen?

Get hands-on with the WQXGA1 touch display with Antimicrobial Corning® Gorilla® Glass,4 boasting an impressive screen-to-body ratio for thin bezels all around.

Is Acer Swift 5 good for gaming?

Acer Swift 5 (2022): Performance Its benchmark scores are better than other laptops with similar specs, so it's more than capable of handling low and modestly demanding games.

How heavy is Acer Swift 5?

The premium, aerospace-grade aluminum chassis is CNC-machined to an incredible 14.95 mm and 1.2 kg. The masterfully crafted design extends to the eco-friendly OceanGlass™ touchpad and a power button featuring a fingerprint reader.


1 Answers

This issue is happening on iOS 13

Add searchController.extendedLayoutIncludesOpaqueBars = true

as well as

`
extension SearchViewController:UISearchControllerDelegate{
    func willPresentSearchController(_ searchController: UISearchController) {
        if #available(iOS 13, *){
            self.navigationController?.navigationBar.isTranslucent = true
        }
    }
    func willDismissSearchController(_ searchController: UISearchController) {
        if #available(iOS 13, *){
            self.navigationController?.navigationBar.isTranslucent = false
        }
    }
}
`
like image 158
Anand Yadav Avatar answered Nov 14 '22 23:11

Anand Yadav