Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying search bar in navigation bar in iOS 8

UISearchDisplayController had a boolean property called displaysSearchBarInNavigationBar. What's the equivalent in iOS 8 to have my search bar move up there? Any guidance is greatly appreciated.

Here's my code, I'm not entirely sure why this isn't working. When I click the search bar, it just disappears instead of moving itself and the navigation bar up.

import UIKit  class ViewController: UIViewController, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource {  let searchController = UISearchController(searchResultsController:  nil)  var tableView = UITableView()  override func viewDidLoad() {     super.viewDidLoad()     // Do any additional setup after loading the view, typically from a nib.      self.searchController.searchResultsUpdater = self     self.searchController.delegate = self     self.searchController.searchBar.delegate = self      self.searchController.hidesNavigationBarDuringPresentation = true     self.searchController.dimsBackgroundDuringPresentation = true      tableView.dataSource = self     tableView.delegate = self       self.navigationItem.titleView = searchController.searchBar       self.definesPresentationContext = true      self.setupSearchBar()     }  override func didReceiveMemoryWarning() {     super.didReceiveMemoryWarning()     // Dispose of any resources that can be recreated. }  func updateSearchResultsForSearchController(searchController: UISearchController) {  }  func setupSearchBar() {      // Set search bar position and dimensions     var searchBarFrame: CGRect = self.searchController.searchBar.frame     var viewFrame = self.view.frame     self.searchController.searchBar.frame = CGRectMake(searchBarFrame.origin.x, searchBarFrame.origin.y + 64,viewFrame.size.width, 44)       // Add search controller's search bar to our view and bring it to forefront     self.view.addSubview(self.searchController.searchBar)  }  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {     var cell = UITableViewCell()      return cell }  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {     return 5 } } 
like image 407
Mihado Avatar asked May 13 '15 23:05

Mihado


People also ask

How do I add a navigation bar to a search box in HTML?

To create a search bar in the navigation bar is easy, just like creating another option in the navbar that will search the database. You need to be careful about the timing of placing the search bar. Make sure separately placed in the navbar.

What is navigation bar in IOS?

A navigation bar appears at the top of an app screen, enabling navigation through a hierarchy of content. A navigation bar also provides a natural place to display a screen's title — helping people orient themselves in your app or game — and it can include controls that affect the screen's content.

How do I customize the search bar in Swift?

Change Search Bar Default Image Color The left hand default search image in UISearchBar represents the left view of the UITextField. The Image is rendered to change it to the desired colour. @IBOutlet weak var searchBar: UISearchBar! Hope it will help you in customising the UISearchBar in your app.


1 Answers

According to Apple :

UISearchDisplayController is deprecated in iOS 8. (Note that UISearchDisplayDelegate is also deprecated.) To manage the presentation of a search bar and display search results in iOS 8 and later, instead use UISearchController.

The UISearchController class defines an interface that manages the presentation of a search bar in concert with the search results controller’s content. The search results controller, a UIViewController object specified by the searchResultsController property, manages the results of the search.

Now you can use the UISearchController to show the search bar in your navigation bar in the following way:

class ViewController: UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {      var searchController : UISearchController!      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.navigationItem.titleView = searchController.searchBar             self.definesPresentationContext = true             }      func updateSearchResults(for searchController: UISearchController) {          }      override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.     } } 

But you have to consider you need to set a UINavigationController like in this Storyboard :

enter image description here

You can do it very easy just select your ViewController and see the following steps:

enter image description here

And then you should see in your device when you make click in the search bar the following picture:

enter image description here

I hope this help you

like image 95
Victor Sigler Avatar answered Oct 31 '22 11:10

Victor Sigler