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:

I hope this help you.