Sebastien
Sebastien

Reputation: 4295

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:

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?

Upvotes: 5

Views: 3486

Answers (3)

Dorukhan Arslan
Dorukhan Arslan

Reputation: 2754

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.

Upvotes: 6

Ramandeep Singh Gosal
Ramandeep Singh Gosal

Reputation: 448

If you are using xcode 9 (ios 11). Then the thing you really want to do is - Use the new Broader navigation bars which are the new highlights in the ios 11 devices. But as there are many who have not shifted to ios-11, previous version devices are also taken into consideration. For adding the search bar to the newer navigation bars I have used the following function which gives a search bar on scrolling and hides it when user scrolls the page.

func addSearchBar() {
if #available(iOS 11.0, *) {
  let sc = UISearchController(searchResultsController: nil)
  sc.delegate = self
  let scb = sc.searchBar
  scb.tintColor = UIColor.white
  scb.barTintColor = UIColor.white
  //Change the colors as you like them
  if let textfield = scb.value(forKey: "searchField") as? UITextField {
    textfield.textColor = UIColor.blue
    if let backgroundview = textfield.subviews.first {

      // Background color
      backgroundview.backgroundColor = UIColor.white

      // Rounded corner
      backgroundview.layer.cornerRadius = 10;
      backgroundview.clipsToBounds = true;
    }
  }

  if let navigationbar = self.navigationController?.navigationBar {
    navigationbar.barTintColor = UIColor.white
  }
  navigationItem.searchController = sc
  navigationItem.hidesSearchBarWhenScrolling = true
}else{
//add the logic for previous version devices here.
}

I have also set the self.navigationController?.navigationBar.prefersLargeTitles = true; in viewDidLoad as there is a bug in xcode9 and setting it from the interface builder does not work(yet).

The following method has been taken from here

Upvotes: 0

Ganesh Manickam
Ganesh Manickam

Reputation: 2139

use this below line of code into your viewDidload

self.navigationController?.navigationBar.isTranslucent = false

Hope this will help you

Upvotes: 3

Related Questions