我已按照谷歌地方自动填充示例将搜索栏添加到导航栏下的视图。它显示确定,但即使使用较大的iOS11导航栏下方的seachbar下面的行隐藏。我在AutoLayout中启用了safeAreaLayoutGuide。
我有我正在使用的代码。
override func viewDidLoad() {
super.viewDidLoad()
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self
navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.prefersLargeTitles = true
searchController = UISearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController
searchController?.searchBar.sizeToFit()
searchController?.hidesNavigationBarDuringPresentation = false
let navBarBounds = self.navigationController!.navigationBar.frame.size
let statusHeight = UIApplication.shared.statusBarFrame.height
let subView = UIView(frame: CGRect(x: 0, y: navBarBounds.height + statusHeight, width: navBarBounds.width, height: 45.0))
// This makes the view area include the nav bar even though it is opaque.
// Adjust the view placement down.
self.extendedLayoutIncludesOpaqueBars = true
self.edgesForExtendedLayout = .top
// When UISearchController presents the results view, present it in
// this view controller, not one further up the chain.
definesPresentationContext = true
subView.addSubview((searchController?.searchBar)!)
view.addSubview(subView)
if #available(iOS 11, *) {
let guide = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
subView.topAnchor.constraintEqualToSystemSpacingBelow(guide.topAnchor, multiplier: 1.0),
guide.bottomAnchor.constraintEqualToSystemSpacingBelow(subView.bottomAnchor, multiplier: 1.0)
])
} else {
let standardSpacing: CGFloat = 8.0
NSLayoutConstraint.activate([
subView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: standardSpacing),
bottomLayoutGuide.topAnchor.constraint(equalTo: subView.bottomAnchor, constant: standardSpacing)
])
}
}
}