UISearchController与状态栏重叠,不会填充iPhone X.

Rov*_*val 6 iphone uisearchbar ios uisearchcontroller ios11

我遇到了很多的问题与iOS 11和显示UISearchController通过提出过来导航栏(如描述这里,例如从苹果教程)

@IBAction func searchAction(sender: UIBarButtonItem) {
    // Create the search controller and specify that it should present its results in this same view        
    searchController = UISearchController(searchResultsController: nil) 

    // Make this class the delegate and present the search
    self.searchController.searchBar.delegate = self
    presentViewController(searchController, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)

它正在隐藏应用程序UINavigationBar并显示UISearchController搜索栏.

问题1.在iOS 11上,它导致搜索栏第一次出现时与状态重叠(再次尝试后不会重叠).

在此输入图像描述 UISearchController第一次呈现.状态栏和搜索栏之间没有空格.

在此输入图像描述 UISearchController再次呈现,UINavigationBar更大,搜索栏是更低的状态栏.

问题2在iPhone X上,它不会覆盖整个空间

在此输入图像描述

我花了好几个小时试图搞清楚.还有其他的,简单的方法是在点击例如后在iOS 11上显示搜索栏.导航栏中的搜索图标?有没有办法修复UISearchControlleriPhone X上的导航栏高度和空间?

Bra*_*sse 1

在 Apple 的“为 iPhone X 构建应用程序”视频中,Apple 建议使用 UINavigationBar 的 searchController 属性,而不是手动显示搜索控制器。

下面是它的工作原理:

if #available(iOS 11, *) {
   self.navigationItem.searchController = searchController;
   searchController.isActive = true;
} else {
   self.present(searchController, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)

请注意,这仅在 iOS 11 中可用。对于早期版本,请执行您已经执行的操作,因为它将继续工作。

如果您进行上述更改,您可能会遇到搜索控制器占据整个屏幕的问题。要解决此问题,您可以在显示 UISearchController 的主 UIViewController 上设置“definesPresentationContext”属性:

//set up UISearchController
self.definesPresentationContext = true;
Run Code Online (Sandbox Code Playgroud)

如果您最终将 DefinesPresentationContext 属性设置为 true,请确保检查它不会干扰 VC 呈现的任何其他 UIViewController。