34

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
}
}
Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
Mihado
  • 1,487
  • 3
  • 17
  • 30

1 Answers1

88

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

Community
  • 1
  • 1
Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
  • 1
    That didn't really do it for me. I posted my code. Feel free to look at it and tell me why this isn't going the way I want it to. – Mihado May 14 '15 at 13:47
  • 1
    @MihadAlzayat I tested it using a navigation bar and it works fine and don't dissapears, but what really you want? put a searchbar underneath the navigation bar (in the top of the tableview)? – Victor Sigler May 14 '15 at 17:06
  • @MihadAlzayat See updated answer to see how to do it using navigation bar as you asked – Victor Sigler May 14 '15 at 17:13
  • Thanks, that's extremey helpful. But what if I have a single view application that doesn't require a navigation controller, but I still want to have that bar at the top of the view? Is that even possible? – Mihado May 14 '15 at 21:45
  • Yes of course you can put you searchbar in your case in the heder of you tableview too if you want – Victor Sigler May 14 '15 at 21:47
  • Also, one last thing! What is acting as my searchResultsController when it is being set to nil? Do I need to create a separate UITableViewController to display the search results in the UITableViewController's UITableView? – Mihado May 14 '15 at 22:46
  • Yes your searchbar is separate from you tableviewcontroller, there a lot of tutorials to know how to do it , it's not difficult. – Victor Sigler May 14 '15 at 22:59
  • @VictorSigler While showing search bar in navigation bar its takes lot of left padding because of "<" back button frame doesn't look nice, How to deal with that? I want to achieve someting like twitter app where the spacing between back button and Search bar is not much – kidsid49 Dec 10 '15 at 06:33
  • Is it possible to make it so that when you click in the search bar, it presents a new view controller behind the search bar and keyboard, rather than just the dimmed version of the view controller the search bar is attached to? – rigdonmr Apr 13 '16 at 20:02
  • Hello, this is working very weel but, how can a I close or hide the searbar? Any idea? – Kepa Santos Mar 30 '17 at 09:33
  • the line: self.navigationItem.titleView = searchController.searchBar is work just fine to me. Here all my code: let searchController = UISearchController(searchResultsController: nil) searchController.dimsBackgroundDuringPresentation = false searchController.searchBar.delegate = self searchController.searchBar.placeholder = "Search" searchController.searchBar.sizeToFit() searchController.hidesNavigationBarDuringPresentation = false self.navigationItem.titleView = searchController.searchBar – xhinoda Jun 28 '17 at 13:11
  • its working but on click on search for searching then search bar changes its position – Deepak Saki Apr 18 '18 at 08:05