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:
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?
4 Answers
Answers 1
use this below line of code into your viewDidload
self.navigationController?.navigationBar.isTranslucent = false
Hope this will help you
Answers 2
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
Answers 3
I think that is your section header that is showing up blank. Try setting the heightForHeaderInSection as 0 and return nil for the viewForHeaderInSection.
Answers 4
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.
0 comments:
Post a Comment