UISearchController 未按预期显示

UISearchController does not display as expected

我正在尝试将 UISearchController 添加到包含 UITableView 的 UIViewController(以及 MKMapView,但希望这不是问题所在)。我关注了 Ray Wenderlich's tutorial,但我无法在行为方面得到相同的结果。

这是我的 viewDidLoad:

override func viewDidLoad() {
        super.viewDidLoad()

        // Setup the Search Controller
        searchController.searchResultsUpdater = self
        searchController.obscuresBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = NSLocalizedString("Search references by project, customer or city", comment: "")
        if #available(iOS 11.0, *) {
            navigationItem.searchController = searchController
            navigationItem.hidesSearchBarWhenScrolling = true
        } else {
            tableView.tableHeaderView = searchController.searchBar
        }
        definesPresentationContext = true

        self.modeSelector.layer.cornerRadius = 5.0

        if let split = splitViewController {
            let controllers = split.viewControllers
            detailViewController = (controllers[controllers.count - 1] as! UINavigationController).topViewController as? ReferenceViewController
        }

        self.navigationItem.rightBarButtonItem?.isEnabled = false
    }

注意中间的#available测试是因为我需要支持iOS到9.1.

现在我看到几个问题:

我看到 Ray Wenderlich 的示例项目的唯一主要区别是,由于我使用 Xcode 9 创建项目,所以我的情节提要不使用顶部和底部布局指南,而是使用安全区域。不知道它是否相关,但这是我唯一看到的。

知道发生了什么事以及如何解决这个问题吗?

在您的 viewDidload

中使用下面这行代码
self.navigationController?.navigationBar.isTranslucent = false

希望对您有所帮助

如果您使用的是 xcode 9 (ios 11)。那么你真正想做的是 - 使用新的 Broader 导航栏,这是 ios 11 设备中的新亮点。但是由于还有很多人没有转ios-11,所以之前版本的设备也考虑在内。 为了将搜索栏添加到较新的导航栏,我使用了以下功能,它在滚动时提供搜索栏并在用户滚动页面时隐藏它。

func addSearchBar() {
if #available(iOS 11.0, *) {
  let sc = UISearchController(searchResultsController: nil)
  sc.delegate = self
  let scb = sc.searchBar
  scb.tintColor = UIColor.white
  scb.barTintColor = UIColor.white
  //Change the colors as you like them
  if let textfield = scb.value(forKey: "searchField") as? UITextField {
    textfield.textColor = UIColor.blue
    if let backgroundview = textfield.subviews.first {

      // Background color
      backgroundview.backgroundColor = UIColor.white

      // Rounded corner
      backgroundview.layer.cornerRadius = 10;
      backgroundview.clipsToBounds = true;
    }
  }

  if let navigationbar = self.navigationController?.navigationBar {
    navigationbar.barTintColor = UIColor.white
  }
  navigationItem.searchController = sc
  navigationItem.hidesSearchBarWhenScrolling = true
}else{
//add the logic for previous version devices here.
}

我也设置了 self.navigationController?.navigationBar.prefersLargeTitles = true; 在 vi​​ewDidLoad 中,因为 xcode9 中存在错误并且从界面构建器中设置它不起作用(目前)。

以下方法取自

如果您需要支持 iOS 到 9.1,您可能使用版本低于 9.1 的模拟器。因此,"maybe" obscuresBackgroundDuringPresentation 不会正确影响 searchController,因为它仅适用于 iOS 9.1 或更新版本。添加 dimsBackgroundDuringPresentation 以支持最高 9.1:

if #available(iOS 9.1, *) {
    searchController?.obscuresBackgroundDuringPresentation = false
} else {
    searchController?.dimsBackgroundDuringPresentation = false
}

如果这不能帮助按预期显示,我几乎可以肯定问题出在您的布局限制上。如果您无法使您的布局符合安全区域,请添加您当前的限制条件。