UISearchController does not display as expected

I am trying to add a UISearchController to a UIViewController that contains a UITableView (and MKMapView too, but hopefully this is not a problem). I followed the Ray Wenderlich tutorial , but I cannot get the same result in terms of behavior.

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
    }

Please note that the #available test in the middle is what I need to support iOS up to 9.1.

Now I see a few problems:

  • The search bar appears immediately, and I cannot hide it by scrolling
  • When I focus the search bar, the top of the table does not snap to the bottom of the navigation element:

enter image description here

, Ray Wenderlich, , , XCode 9, , . , , , .

, ?

+4
4

iOS 9.1, , , 9.1. , "" obscuresBackgroundDuringPresentation searchController , iOS 9.1 . dimsBackgroundDuringPresentation 9.1:

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

, , , . , , .

+1

viewDidload

self.navigationController?.navigationBar.isTranslucent = false

,

+3

xcode 9 (ios 11). , , - , ios 11. , ios-11, . , , , .

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.
}

self.navigationController?.navigationBar.prefersLargeTitles = true; viewDidLoad, xcode9 ().

0

I think this is the title of your section that appears blank. Try setting heightForHeaderInSection to 0 and return nil for viewForHeaderInSection.

0
source

Source: https://habr.com/ru/post/1689770/


All Articles