1

我已按照 google 地方自动完成示例将搜索栏添加到导航栏下的视图中。它显示正常,但即使搜索栏下方的线隐藏在较大的 iOS11 导航栏下。我在 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)
     ])
  }
 }
}
4

0 回答 0