Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchController does not display as expected

I'm trying to add a UISearchController to a UIViewController that contains a UITableView (and an MKMapView too, but hopefully that's not the problem). I followed Ray Wenderlich's tutorial but I can't get the same result in terms of behaviour.

Here is my viewDidLoad:

override func viewDidLoad() {
        super.viewDidLoad()

        // Setup the Search Controller
        searchController.searchResultsUpdater = self
        searchController.obscuresBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = NSLocalizedString("Search references by project, customer or city", comment: "")
        if #available(iOS 11.0, *) {
            navigationItem.searchController = searchController
            navigationItem.hidesSearchBarWhenScrolling = true
        } else {
            tableView.tableHeaderView = searchController.searchBar
        }
        definesPresentationContext = true

        self.modeSelector.layer.cornerRadius = 5.0

        if let split = splitViewController {
            let controllers = split.viewControllers
            detailViewController = (controllers[controllers.count - 1] as! UINavigationController).topViewController as? ReferenceViewController
        }

        self.navigationItem.rightBarButtonItem?.isEnabled = false
    }

Note that the #available test in the middle is because I need to support iOS up to 9.1.

Now I see several problems:

  • The search bar appears right away and I can't hide it by scrolling
  • When I focus the search bar, the top of the tableview doesn't stick to the bottom of the navigation item:

enter image description here

The only major difference I see with Ray Wenderlich's sample project is that since I created my project with Xcode 9, my storyboard doesn't use top and bottom layout guides, but safe areas. Don't know if it's relevant, but that's the only thing I see.

Any idea what's going on and how I could fix this?

like image 777
Sebastien Avatar asked Nov 24 '17 09:11

Sebastien


2 Answers

If you need to support iOS up to 9.1, you probably use emulator with version older than 9.1. Therefore, "maybe" obscuresBackgroundDuringPresentation doesn't affect the searchController properly, since it's only available on iOS 9.1 or newer. Add dimsBackgroundDuringPresentation to support up to 9.1:

if #available(iOS 9.1, *) {
    searchController?.obscuresBackgroundDuringPresentation = false
} else {
    searchController?.dimsBackgroundDuringPresentation = false
}

If this doesn't help to display as expected, I'm almost sure that the problem is about your layout constraints. Please add your current constraints if you couldn't bring your layout in compliance with safe area.

like image 108
Dorukhan Arslan Avatar answered Nov 15 '22 05:11

Dorukhan Arslan


use this below line of code into your viewDidload

self.navigationController?.navigationBar.isTranslucent = false

Hope this will help you

like image 43
Ganesh Manickam Avatar answered Nov 15 '22 07:11

Ganesh Manickam