Cómo agregar SearchController.searchBar en la barra de navigationBar (UITableView) swift

Intento agregar SearchController.searchBar debajo de la barra de navigation.

override func viewDidLoad() { super.viewDidLoad() searchController = UISearchController(searchResultsController: nil) searchController.searchResultsUpdater = self searchController.searchBar.sizeToFit() searchController.hidesNavigationBarDuringPresentation = false searchController.delegate = self searchController.dimsBackgroundDuringPresentation = false // searchController.searchBar.delegate = self let SearchFrame = CGRectMake(0,(self.navigationController?.navigationBar.frame.height)!, (self.navigationController?.navigationBar.frame.width)!,searchController.searchBar.frame.height) searchController.searchBar.frame = SearchFrame self.navigationController?.navigationBar.addSubview(searchController.searchBar) } 

Este código agrega un searchBar bajo la barra de navigation, pero searchBar no puede detectar la interacción del usuario. ¡No puedo hacer clic en el campo de input de text de búsqueda! ¿Qué hice mal?

Prueba esto. Esto funciona para mi

  var SearchFrame : CGRect? override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let searchController = UISearchController(searchResultsController: nil) searchController.searchResultsUpdater = self searchController.searchBar.sizeToFit() searchController.hidesNavigationBarDuringPresentation = false searchController.delegate = self searchController.dimsBackgroundDuringPresentation = false // searchController.searchBar.delegate = self SearchFrame = CGRectMake(0,(self.navigationController?.navigationBar.frame.height)!, (self.navigationController?.navigationBar.frame.width)!,searchController.searchBar.frame.height) searchController.searchBar.frame = SearchFrame! self.navigationController?.navigationBar.addSubview(searchController.searchBar) } override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) var navbarFrame = self.navigationController!.navigationBar.frame navbarFrame.size = CGSize(width: navbarFrame.width, height: navbarFrame.height + SearchFrame!.height) self.navigationController?.navigationBar.frame = navbarFrame } 

¡Mi problema fue resuelto!

¡Primero necesitamos crear un file XIB con class y agregar dos NavigationBar a ellos! ¡Entonces necesita agregar un IBOutlets! ¡Dos barras de navegación en xib!

Luego escribe el código:

 var TitleNavigationBar = UINavigationBar() //This need to add Title and Subtitle to NavigationBarTitleView override func viewDidLoad() { super.viewDidLoad() let NavBarWithSearch = NSBundle.mainBundle().loadNibNamed("NavBarWithSearchBar", owner: self, options: nil)[0] as! NavBarWithSearchBar self.navigationController?.navigationBar.frame = CGRectMake((self.navigationController?.navigationBar.frame.origin.x)!, 0.0, (self.navigationController?.navigationBar.frame.width)!, (self.navigationController?.navigationBar.frame.height)! + self.searchController.searchBar.frame.height) NavBarWithSearch.frame = CGRectMake(0.0,0.0, (self.navigationController?.navigationBar.frame.width)!, (self.navigationController?.navigationBar.frame.height)!) NavBarWithSearch.NavigationBarForSearchBar.topItem?.titleView = searchController.searchBar TitleNavigationBar = NavBarWithSearch.CustomNavigationBar //=======This need to add Title and Subtitle to NavigationBarTitleView` self.navigationController?.navigationBar.addSubview(NavBarWithSearch) self.automaticallyAdjustsScrollViewInsets = false tableView.contentInset = UIEdgeInsetsMake((self.navigationController?.navigationBar.frame.height)! + UIApplication.shanetworkingApplication().statusBarFrame.height, 0, (self.tabBarController?.tabBar.frame.height)!, 0) searchController.hidesNavigationBarDuringPresentation = false } override func viewWillAppear(animated: Bool) { //This need to add Title and Subtitle to NavigationBarTitleView let titleView = NSBundle.mainBundle().loadNibNamed("NavTitleView", owner: self, options: nil)[0] as! NavTitleView titleView.TitleLabel.text = "Title" titleView.contentMode = UIViewContentMode.ScaleAspectFit titleView.SubtitleLabel.text = "SubTitle" let TitleViewFrame = CGRectMake((UIScreen.mainScreen().bounds.width/2) - 80, 0, 160, 44) titleView.frame = TitleViewFrame TitleNavigationBar.topItem?.titleView = titleView //========This need to add Title and Subtitle to NavigationBarTitleView self.navigationController?.navigationBar.frame = CGRectMake((self.navigationController?.navigationBar.frame.origin.x)!, 20, (self.navigationController?.navigationBar.frame.width)!, (self.navigationController?.navigationBar.frame.height)! + self.searchController.searchBar.frame.height) } 

El file XIB con class para NavBarTitleView con subtítulos debería ser similar a este: Título y subtítulo

¡Finalmente obtengo una hermosa barra de navigation con barra de búsqueda!

Navbar con entrada de texto de la barra de búsqueda

PS searchController.hidesNavigationBarDuringPresentation = true // ¡Esto no funciona porque la barra de navigation personalizada se agregó a la barra de navigation principal como subvista!