I'm experimenting with the new UISearchController
API introduced in iOS 8. Although the API is new, it uses the same old UISearchBar
. I added it to the UINavigationController
's titleView
.
import UIKit
class ViewController: UIViewController, UISearchBarDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.delegate = self
navigationItem.titleView = searchController.searchBar
}
}
I don't need the search bar to take the entire width of the navigation bar. The problem is I can't resize it. First I tried setting the search bar's frame.
searchController.searchBar.frame = CGRect(x: 0, y: 0, width: 100, height: 44)
That didn't work so I tried setting the titleView
's frame.
navigationItem.titleView!.frame = CGRect(x: 0, y: 0, width: 100, height: 44)
Neither of them works.
Does anyone know another way to do this?
Thank you.