4

I have researched this problem a lot but I have found no solution that would work for me.

Basically, I have a UIViewController that presents UISearchController like this:

let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.delegate = self
searchController.dimsBackgroundDuringPresentation = false

searchController.searchBar.delegate = self
view.addSubview(searchController.searchBar)

User is then expected to tap the UISearchBar to present searchController and reveal the keyboard. However, a strange thing happens during the transition between controllers.

Overlap

It seems as if the UISearchController didn't cover the status bar and let you see the UIViewController that presented it below. I would like to find a way to prevent this, i.e. to force the search controller to extend all the way under the status bar.

Things that I have already done:

  • I have set self.definesPresentationContext = true in viewDidLoad:.
  • I have found out that this is a known bug, namely rdar://20942583.
  • I have attempted to circumvent the bug by setting:

    self.edgesForExtendedLayout = .All
    self.extendedLayoutIncludesOpaqueBars = true
    

    It didn't work.

I'm running out of ideas. Please help.

Thanks a bunch, Pete.

Petr Mánek
  • 1,046
  • 1
  • 9
  • 24

2 Answers2

0

Facing the same issue and tried everything from here and here and none of this worked for me :(

Best workaround that is working (ugly I know) until I find a better solution:

override func viewDidLoad() {
    super.viewDidLoad()
    searchController.delegate = self
}

func willPresentSearchController(searchController: UISearchController) {

    let statusHeight = UIApplication.sharedApplication().statusBarFrame.size.height

    if bgBar == nil {
        bgBar = UIView(frame: CGRectMake(0, 0, view.frame.width, (navigationController?.navigationBar.frame.height)! + statusHeight))
        bgBar.backgroundColor = UIColor.redColor()
        view.addSubview(bgBar)
    } else {
        bgBar.hidden = false
    }

    tableView.contentInset.top = statusHeight
}

func willDismissSearchController(searchController: UISearchController) {

    bgBar.hidden = true
    tableView.contentInset.top = 0
}
Community
  • 1
  • 1
Ana
  • 1
  • 1
0

On swift 4:

viewWillAppear(_ animated: Bool) {
   let statusHeight = UIApplication.shared.statusBarFrame.size.height
   let sbview = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: statusHeight))
   sbview.backgroundColor = .white
   view.addSubview(sbview)
{
Serhii Didanov
  • 2,200
  • 1
  • 16
  • 31