0

I have a simple app with a TableView but without a NavigationBar/NavigationController. The app also has a UISearchController with a SearchBar that is always visible. I tried to hide the status bar with prefersStatusBarHidden and that works fine. Until the SearchBar is activated. Then the status bar will appear again.

How can I prevent this and keep the status bar hidden?

var cityRepository:CityRepository?
var searchController:UISearchController?

override func viewDidLoad()
{
    self.cityRepository = CityRepository()
    self.searchController = UISearchController(searchResultsController: nil)
    self.searchController!.searchResultsUpdater = self
    self.searchController!.dimsBackgroundDuringPresentation = false
    self.definesPresentationContext = true
    self.tableView.tableHeaderView = searchController?.searchBar

    //* Already tried this ....
    self.edgesForExtendedLayout = UIRectEdge.None
    self.extendedLayoutIncludesOpaqueBars = true
    self.automaticallyAdjustsScrollViewInsets = false

    super.viewDidLoad()
}

override func prefersStatusBarHidden() -> Bool
{
    return true
}
TalkingCode
  • 13,407
  • 27
  • 102
  • 147
  • Do you want it always hidden? You can add an entry to your plist as per something like this: http://stackoverflow.com/questions/22299214/iphone-ios-status-bar-not-hiding-in-xcode-project – yesthisisjoe Feb 22 '16 at 21:22
  • When I add UIViewControllerBasedStatusBarAppearance as false and UIStatusBarHidden as true the status bar is always hidden on any view. That will do but it is not exactly what I had in mind. Would be great to set this per View Controller. – TalkingCode Feb 23 '16 at 20:08

1 Answers1

0

Swift 3

To selectively display the status bar, you must implement the following:

Go to the Info.plist, add 'View controller-based status bar appearance' -> YES. This will give you access to being able to set the appearance based on the state of the prefersHiddendStatusBar variable for your specific view.

The settings in Interface Builder are simply for simulated metrics; that is, what displays in StoryBoard objects while using Interface Builder.

Next, you will need to create a way to store your conditional preference: "Do I want to display the status bar now?" In your view controller, create a boolean variable to hold this preference:

var displayStatusBar: Bool = false

Then, when you use the SearchController, you must tie into the specific delegate methods that fire when you interact with the searchbar. I recommend using:

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar)

and

func searchBarCancelButtonClicked(_ searchBar: UISearchBar)

In each of those delegate methods, you can set the displayStatusBar variable to true or false and then use the setNeedsStatusBarAppearanceUpdate() in each method. This will force a reload in the status bar. If you think it looks choppy, throw that code inside a UIView.animate(withDuration:_) completion block for a nice and smooth visual change.

Finally, you need to override the View's preferred status variable and set it to the preference variable.

override var prefersStatusBarHidden: Bool {
    return hideStatusBar
}
Mark Filter
  • 353
  • 5
  • 8