UISearchBar out of screen bounds when navigation b

2019-04-11 06:29发布

问题:

I try add UISearchBarController to tableView, but when i set UINavigationBar.appearance().translucent = false then UISearchBar hide out of screen

in TableViewController

var resultSearchC: UISearchController = UISearchController()

override func viewDidLoad() {
    super.viewDidLoad()

    self.resultSearchC = ({
        let c = UISearchController(searchResultsController: nil)
        c.searchResultsUpdater = self

        c.searchBar.translucent = true

        c.searchBar.barTintColor =  .redColor()
        c.searchBar.sizeToFit()

        self.tableView.tableHeaderView = c.searchBar

        return c
    })()

    // self.edgesForExtendedLayout = .None

    self.tableView.reloadData()
}

AppDelegate:

let navBarAppearance = UINavigationBar.appearance()

    navBarAppearance.barStyle = .Black
    navBarAppearance.translucent = false

回答1:

You need to set extendedLayoutIncludesOpaqueBars to true in viewDidLoad().

extendedLayoutIncludesOpaqueBars = true;