Baoqiang Yan
Baoqiang Yan

Reputation: 41

Extra space in the background of UISearchBar causing UISearchBar overlap with status bar

This problem is essentially that, UISearchBar is overlapping with the status bar when activated. I know that there are already many similar problems posted and I have tried all means but I still could not get it solved.

When the screen is initialized, the search bar seems in the right position. When I scroll down the screen a bit, you can see the background of the search bar is taller than normal as compared with the one I got from the mail application on iPhone.

When screen is scrolled down a bit

The height of the extra space in the search bar background is same as the status bar height. I believe this is what caused the following problem, namely when search bar is activated, it overlaps with the status bar. But when search cancelled, it is placed back to normal place as shown in the first picture.

Search bar overlaps with status bar when activated.

This is my code.

Global Var:

    let searchController = UISearchController(searchResultsController: nil)

In viewDidLoad:

    self.extendedLayoutIncludesOpaqueBars = true
    self.definesPresentationContext = true
    self.navigationController?.navigationBar.isHidden = false
    self.originalFrame = self.tabBarController?.tabBar.frame
    searchController.hidesNavigationBarDuringPresentation = true
    searchController.searchBar.barTintColor = Settings.tealColor
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.scopeButtonTitles = ["All", "Pre-order", "Delivery", "Carry-out", "Dine-in", "History"]
    searchController.searchBar.delegate = self
    searchController.searchBar.sizeToFit()
    restaurantTableView.tableHeaderView = searchController.searchBar

Your any help will be greatly appreciated. I have worked on this problem for two days. Thanks

Upvotes: 0

Views: 271

Answers (1)

Baoqiang Yan
Baoqiang Yan

Reputation: 41

Funny enough, 30 minutes after I posted the problem which took me two days already, I figured this out by myself. The culprit was the following block of code I did not remove after I switched from customized navigation bar to the built-in navigation bar.

func position(for bar: UIBarPositioning) -> UIBarPosition {
    return UIBarPosition.topAttached
}

Thanks for your attention, anyway. Hope someone who is having the same problem can benefit here.

Upvotes: 0

Related Questions