1

How to add search bar in navigation bar for iPhone? right now I added to table view?

SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
ram
  • 83
  • 2
  • 5

1 Answers1

4

In iOS 7 it's very easy. Simply add this to your UISearchDisplayController

searchDisplayController.displaysSearchBarInNavigationBar = YES;
DiscDev
  • 38,652
  • 20
  • 117
  • 133
  • 1
    Hey, kinda great, but my search bar is displayed in the middle of the screen.. and not in the navigation bar. – Van Du Tran Oct 05 '13 at 15:53
  • @VanDuTran - the only way I can see that happening is if you have left over code somewhere in your class that modifies the positioning of the search bar. I've used this in a couple apps targeting iOS 7 and this works fine. See the apple docs: https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TransitionGuide/Bars.html – DiscDev Oct 05 '13 at 19:14
  • 2
    @VanDuTran I had the same problem and fixed it by creating an outlet from the NavigationBar titleView to the SearchBar in IB. You can also do it programmatically with << self.navigationItem.titleView = self.searchDisplayController.searchBar; >>. Also, I had to reset the tableView contentInset top by -44 to fix the visible white space where the search bar existed. – lm2s May 21 '14 at 22:19
  • @lm2s I have a follow up question about doing this in the IB, will you please take a look: http://stackoverflow.com/questions/24808818/how-to-add-uisearchbar-to-navigationbar-through-the-storyboard-in-ios – learner Jul 17 '14 at 16:36