Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView with UISearchController go under navbar when enter in a result view and come back

I have a UITableView with a UISearchController search bar in the UINavigationBar, all works perfectly, but when I push a result of the searched results of the UISearchController, and I come back the UITableView is under the NavBar, this is how I initialize the UISearchController:

        self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
        self.searchController.delegate = self;
        self.searchController.searchResultsUpdater = self;

        self.searchController.searchBar.delegate = self;
        self.searchController.dimsBackgroundDuringPresentation = NO;
        self.searchController.hidesNavigationBarDuringPresentation = NO;
        self.searchController.searchBar.placeholder = NSLocalizedString(@"Local Search", @"");
        self.searchController.searchBar.frame = CGRectMake(0, -5, [UIScreen mainScreen].bounds.size.width, 44);


        ctrl = [[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 44)];
        ctrl.backgroundColor = [UIColor clearColor];
        ctrl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [ctrl addSubview:self.searchController.searchBar];
        self.navigationItem.titleView = ctrl;
        self.definesPresentationContext = YES;

The search bar is displayed perfectly in the UINavigationBar, then when I search something and I push the view controller of one results like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    DetailListController *detailList = [[DetailListController alloc] init];
    [self.navigationController pushViewController:detailList animated:YES];
}

when I come back to the UITableView doing this:

[self.navigationController popViewControllerAnimated:YES];

the UITableView is under the UINavigationBar, how I can fix this?

thanks

like image 374
Piero Avatar asked Oct 08 '15 09:10

Piero


3 Answers

I had exactly the same problem, setting extendedLayoutIncludesOpaqueBars to YES/true in view controller, that presents search controller, seems to fix it.

self.extendedLayoutIncludesOpaqueBars = YES

As a word of caution: setting this property changes value of scroll view's vertical content offset.

like image 87
Piotrek Wilczyński Avatar answered Sep 18 '22 15:09

Piotrek Wilczyński


This is an extremely frustrating bug in UIKit. What appears to be happening is that the presenting view controller's top layout guide get reset to 0, meaning it is now underneath the nav bar. The layout guide is a read only property, so you can't fix it by editing directly. However, I did come up with a hack to get it to reset to the correct value. Add this to your UISearchControllerDelegate:

- (void)didDismissSearchController:(UISearchController *)searchController
{
    UINavigationController *nav = self.navController; // you muse save this earlier

    // force to layout guide to reset by pushing a dummy controller and popping it right back
    [nav pushViewController:[UIViewController new] animated:NO];
    [nav popViewControllerAnimated:NO];
}
like image 44
Oz Solomon Avatar answered Sep 19 '22 15:09

Oz Solomon


I have used UISearchController for years but today is the first time I am facing this issue I used to set edgesForExtendedLayout = [] and extendedLayoutIncludesOpaqueBars = true and everything is fine

but today It is not, I needed to reverse it to edgesForExtendedLayout = .all it may be useful to you too!

like image 26
engali Avatar answered Sep 17 '22 15:09

engali