我的SearchController有意外的行为,在我的滚动内容之上显示半透明的内容:
理想情况下,我希望它随内容一起向上滚动,并使导航栏折叠起来。
有关如何实现此目标的任何提示?
我正在ViewDidLoad中创建我的SearchController,如下所示:
let searchResultsController = SearchViewController(nibName: "SearchViewController", bundle: nil)
let searchController = UISearchController(searchResultsController: searchResultsController)
searchController.searchResultsUpdater = searchResultsController
searchController.searchBar.autocapitalizationType = .sentences
searchController.obscuresBackgroundDuringPresentation = true
searchController.searchBar.placeholder = "Search Birds"
searchController.searchBar.delegate = self
navigationItem.searchController = searchController
definesPresentationContext = true
在ViewWillAppear中,我设置了NavigationController:
self.navigationController!.setNavigationBarHidden(false, animated: true)
我在AppDelegate中创建导航控制器,如下所示: 公共函数applicationDidFinishLaunching(_ application:UIApplication){ //实例化初始控制器 让initialViewController = HomeViewController(nibName:“ HomeViewController”,bundle:nil)
let navigationController = UINavigationController(rootViewController: initialViewController)
答案 0 :(得分:1)
您可以尝试(如果您的应用在iOS 11.0和更高版本上运行)
override func viewDidAppear(_ animated: Bool) {
if #available(iOS 11.0, *) {
navigationItem.hidesSearchBarWhenScrolling = true
}
}
Apple文档:
如果此属性为true(默认设置),则searchController的搜索 当用户在顶视图控制器的滚动条中滚动时,该条将隐藏 视图。如果为false,则搜索栏将保持可见并固定 在导航栏下方。
答案 1 :(得分:1)
您可以尝试以下操作:
override func viewWillAppear(_ animated: Bool) {
if #available(iOS 11.0, *) {
navigationItem.hidesSearchBarWhenScrolling = false
}
}
override func viewDidAppear(_ animated: Bool) {
if #available(iOS 11.0, *) {
navigationItem.hidesSearchBarWhenScrolling = true
}
}
答案 2 :(得分:0)
navigationItem.hidesSearchBarWhenScrolling = true
答案 3 :(得分:0)
让您的控制器确认UIScrollViewDelegate并覆盖以下方法:
extension ViewController: UIScrollViewDelegate{
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
self.navigationController!.setNavigationBarHidden(true, animated: true)
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
self.navigationController!.setNavigationBarHidden(false, animated: true)
}
}
这可能需要根据您在应用程序中寻找的美观程度进行一些更改。