adding searchBar to navigationBar in swift

Try this code that worked for me:

 lazy   var searchBars:UISearchBar = UISearchBar(frame: CGRectMake(0, 0, 200, 20))

override func viewDidLoad() {
    super.viewDidLoad()
    var leftNavBarButton = UIBarButtonItem(customView: searchBars)
    self.navigationItem.leftBarButtonItem = leftNavBarButton
}

Try this

lazy var searchBar = UISearchBar(frame: .zero)
override func viewDidLoad() {
    super.viewDidLoad()
 navigationItem.titleView = searchBar} 

You can use this

private let searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {

 self.navigationItem.searchController = searchController
 self.navigationItem.hidesSearchBarWhenScrolling = false
}