0
votes

Summary

UINavigationController is showing broken animation when a child UIViewController has a UISearchController embedded into the navigation item's search controller.

This only happens if I set the UISearchController in the navigation item.

In the image below there are 2 examples:

  1. Change Location ViewController - has animation lag when clicking on the Back (Settings) button.
  2. Customize ViewController - works fine.

Flow

UITableViewController > UIViewController with UISearchController embeded inside the navigation item

image description

Findings

I have researched this behavior and found some answers that described a similar behavior but not exactly the same as I have setup.

Trying to implement a solution suggested in the below post by setting the navigation item search controller to nil - did not solve this behavior:

Broken UISearchBar animation embedded in NavigationItem

The code is below. Thanks in advance.

class ChangeLocationViewController: UIViewController {

    // MARK: - Outlets
    @IBOutlet weak var locationBanner: CustomView!
    @IBOutlet weak var locationNameLabel: UILabel!
    @IBOutlet weak var locationTimeLabel: UILabel!
    @IBOutlet weak var mapView: MKMapView!

    let loadingBanner = LoadingBanner()

    var resultsViewController: GMSAutocompleteResultsViewController?
    var searchController: UISearchController?
    let locationManager = LocationManager.shared

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.locationManagerDelegate = self

        GMSPlacesClient.provideAPIKey(AppSettings.googleAPIKey)

        self.definesPresentationContext = true;


        resultsViewController = GMSAutocompleteResultsViewController()
        resultsViewController?.delegate = self

        let autoCompletedFilter = GMSAutocompleteFilter()
        autoCompletedFilter.type = .city
        resultsViewController?.autocompleteFilter = autoCompletedFilter

        searchController = UISearchController(searchResultsController: resultsViewController)
        searchController?.searchResultsUpdater = resultsViewController
        searchController?.hidesNavigationBarDuringPresentation = false
        searchController?.searchBar.placeholder = "Search a place".localized
        searchController?.delegate = self

        // Setting the search controller [when it is not set, everything works great :)]
        navigationItem.searchController = searchController
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        // Removing the search controller
        self.navigationItem.searchController = nil
    }
}
1
have you tried the code self.navigationItem.searchController = nil in viewDidDisappear instead of viewWillDisappear ?chirag90
Same results. Thanks.Sam

1 Answers

0
votes

Set navigationItem.searchController to nil when the other view controller appears as well.

class ChangeLocationViewController: UIViewController {
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        /* searchController */
        searchController.isActive = false
        navigationItem.searchController = nil
    }
}

class SettingsTableController: UITableViewController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        /* searchController */
        navigationItem.searchController = nil
    }
}