UISearchController searchBar in tableHeaderView animating out of the screen

时光毁灭记忆、已成空白 提交于 2019-12-03 18:57:27

问题


I have a UISearchController with a UITableViewController as a searchResultsController, the UISearchBar of this searchController is set to be in the tableHeaderView of my current tableView displayed in my root ViewController. Almost everything is working well, as expected. But in the animation of the UISearchBar (When i click on the searchBar and the UINavigationBar hides and the searchBar goes to the top, as in the UISearchDisplayController) i have a strange behavior. Instead of moving to the position of the UINavigationBar (y: 0), it jumps out of the screen and than starts the animation that shows the cancel button. I tried moving my instantiate code to the viewDidLoad instead of init, and the things are just the same. I think that the center of the problem is in the frame of the searchResultsController's view, but i'm not sure about that(I tried setting the frame, without success). Everything that i am doing is in pure code.

Here is the relevant part of the code:

- (void) viewDidLoad { 
    [super viewDidLoad];

    // search controller setup
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultsController];
    self.searchController.delegate = self;
    self.searchController.searchResultsUpdater = self;
    self.searchController.searchBar.delegate = self;

    [self.searchController.searchBar sizeToFit];
    self.tableView.tableHeaderView = self.searchController.searchBar;

    self.searchController.definesPresentationContext = YES;
}

And i have a lazy load for the searchResultsController:

- (UITableViewController *)searchResultsController {
    if (_searchResultsController == nil) {
        _searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
        _searchResultsController.tableView.delegate = self;
        _searchResultsController.tableView.dataSource = self;
    }
    return _searchResultsController;
}

I've downloaded the sample code from apple, but they use storyBoards and a xib for UITableViewCell, the SearchController works perfectly in the project. Does anyone had the same problem? How can i fix this? Any solutions or suggestions would be appreciated.

Thanks for the attention.


回答1:


Add

self.extendedLayoutIncludesOpaqueBars = YES;

on viewDidLoad method




回答2:


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;



回答3:


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

self.definesPresentationContext = YES;



回答4:


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
}



回答5:


In Swift, try:

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

    // ...
}



回答6:


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.




回答7:


SWIFT 3.01

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

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



回答8:


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.



来源:https://stackoverflow.com/questions/26222671/uisearchcontroller-searchbar-in-tableheaderview-animating-out-of-the-screen

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