UISearchController doesn't work properly with a non-translucent UINavigationBar

我怕爱的太早我们不能终老 提交于 2019-11-28 09:41:23

It's clearly a bug (rdar://20942583).

My workaround is to set

self.edgesForExtendedLayout = UIRectEdgeAll;
self.extendedLayoutIncludesOpaqueBars = YES;

This allows you to keep the navigation bar opaque. The downside is that the content flows below the bar even if it can't be seen, creating some overhead.

All I needed was:

func viewDidLoad() { 

    extendedLayoutIncludesOpaqueBars = true
}

One workaround for this is to make the status bar translucent just before the search is going to become active, and remove the translucency when the search is about become inactive.

You can do this by registering your view controller as a delegate of UISearchController, and implementing the willPresentSearchController and willDismissSearchController methods. For example (in Swift):

Declare your view controller as a delegate of UISearchController:

 class MyViewController: UITableViewController, UISearchControllerDelegate

Don't forget to actually set it as the delegate, for instance in viewDidLoad add:

    searchController.delegate = self

And finally:

func willPresentSearchController(searchController: UISearchController) {
    navigationController?.navigationBar.translucent = true
}

func willDismissSearchController(searchController: UISearchController) {
    navigationController?.navigationBar.translucent = false
}

if someone have a problem like non-translucent hidden the search bar u can just had this :

self.definesPresentationContext = true

Regards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!