Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

displaysSearchBarInNavigationBar deprecated in iOS8

I'm trying to find an alternative to displaysSearchBarInNavigationBar for iOs8, is there anything I can use (in swift)?

I tried self.navigationItem.titleView = resultSearchController.searchBar but it doesn't do anything.

I don't understand how to find the replacement for functions that are deprecated, on the apple documentation it just say deprecated with no alternative, if you have any tips it would be greatly appreciated.

like image 681
Kali Aney Avatar asked Jun 02 '15 21:06

Kali Aney


People also ask

What is search display controller?

An object that manages the display of a search bar, along with a table view that displays search results.

What is navigation bar in iOS?

A UINavigationBar object is a bar, typically displayed at the top of the window, containing buttons for navigating within a hierarchy of screens. The primary components are a left (back) button, a center title, and an optional right button.

How to change navbar color SwiftUI?

To change a navigation bar color in SwiftUI, you apply toolbarBackground modifier to the content view of NavigationStack . NavigationView is deprecated in iOS 16. toolbarBackground accepts two parameters. ShapeStyle : The style to display as the background of the bar.


1 Answers

You can put an UISearchBar in a UINavigationBar without using a UINavigationController without problem, but you have to do a minor change only, first you need to define an @IBOutlet to the UINavigationItem inside your UINavigationBar but its name need to be different from the navigationItem property defined in all the UIViewController class, see the following code:

class ViewController: UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {

    var searchController : UISearchController!

    @IBOutlet weak var navigationItemBar: UINavigationItem!

    override func viewDidLoad() {
       super.viewDidLoad()

       self.searchController = UISearchController(searchResultsController:  nil)

       self.searchController.searchResultsUpdater = self
       self.searchController.delegate = self
       self.searchController.searchBar.delegate = self

       self.searchController.hidesNavigationBarDuringPresentation = false
       self.searchController.dimsBackgroundDuringPresentation = true

       self.navigationItemBar.titleView = searchController.searchBar

       self.definesPresentationContext = true        
    }

    func updateSearchResultsForSearchController(searchController: UISearchController) {

    }

    override func didReceiveMemoryWarning() {
       super.didReceiveMemoryWarning()
       // Dispose of any resources that can be recreated.
    }
}

And then you can see something like this in your simulator:

enter image description here

I hope this help you.

like image 169
Victor Sigler Avatar answered Oct 02 '22 17:10

Victor Sigler