6

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.

Kalianey
  • 2,738
  • 6
  • 25
  • 43
  • Try UISearchBar: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchBar_Class/index.html#//apple_ref/c/tdef/UISearchBarStyle – Horatio Jun 02 '15 at 21:53
  • I've been there, I don't see anything replacing displaysSearchBarInNavigationBar – Kalianey Jun 02 '15 at 22:18
  • @KaliAney I post an answer about it in this http://stackoverflow.com/questions/30226835/displaying-search-bar-in-navigation-bar-in-ios-8/30227166#30227166 – Victor Sigler Jun 02 '15 at 23:05
  • Thank you for your answer @VictorSigler, the only problem is I don't use a navigationController and cannot use it there because it messes up with other parts of my code, any other way to do it without it? – Kalianey Jun 02 '15 at 23:12
  • @KaliAney See my answer – Victor Sigler Jun 02 '15 at 23:52

1 Answers1

8

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.

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
  • I try exactly this on iOS 10 but In the end I have a search bar in my navigation item that does not become first responder. – plaetzchen Feb 22 '17 at 15:53