UISearchController searchBar in tableHeaderView animating out of the screen

半腔热情 提交于 2019-11-30 02:53:29

Add

self.extendedLayoutIncludesOpaqueBars = YES;

on viewDidLoad method

Piet Grootnoten

Have you tried to set the hidesNavigationBarDuringPresentation to false? Solved my headache..

self.searchController.hidesNavigationBarDuringPresentation = false;

Putting the searchbar in the navigation bar gives a more solid user experience in my opinion (for iphone)

self.navigationItem.titleView = self.searchController.searchBar;

To make this clearer @Lorenzo's answer worked for me.

self.definesPresentationContext = YES;

Try this out:

First you need to delegate the

UISearchControllerDelegate

For Swift

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

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

In Swift, try:

override func viewDidLoad() {
    edgesForExtendedLayout = []
    searchController.hidesNavigationBarDuringPresentation = false

    // ...
}

I noticed that the UISearchController works perfectly in one of the my views but not the other. The problem was with the UITableViewController and not the UIViewController. If you switch to a UIViewController with a UITableView inside it and properly constrained there are no issues. I implemented mine with a XIB and it worked perfectly.

SWIFT 3.01

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

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

In my case the searchBar was in the tableHeaderView and there was no NavigationBar on screen. But the SearchBar still animated upwards overlapping the status bar when becoming active. The solution to prevent this was to set:

searchController.hidesNavigationBarDuringPresentation = false

Which is weird because as I said the view controller was not using a navigation bar.

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