UISearchBar在导航栏半透明= false时超出屏幕界限

我尝试添加UISearchBarController到tableView,但是当我设置UINavigationBar.appearance()。translucent = false然后UISearchBar隐藏在屏幕之外

在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 

您需要在viewDidLoad()中将extendedLayoutIncludesOpaqueBars设置为true。

 extendedLayoutIncludesOpaqueBars = true;