简体   繁体   中英

UISearchController searchbar in a tableview header leaves status bar sized gap

Im using a UIViewController with a tableView. I've setup a searchcontroller and embed the searchbar into the tableview header. When i search it leaves a status bar sized gap between the tableview and the uiview above it. I realize there are a lot of other questions on SO about this but none of them solved this problem.

Here is the code for setting up the searchbar:

self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = YES;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.backgroundImage = [UIImage new];
self.searchController.searchBar.backgroundColor = kBlueNavBarColor;
self.searchController.searchBar.tintColor = [UIColor whiteColor];

self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;

在此处输入图片说明 在此处输入图片说明 这是视图的布局:

I had a similar issue when showing a UIViewController modally. The navigation bar was going up too far. I resized the navigation bar made it a little bit wider. Let me know if this helps. This issue started with iOS 7. You might not need to check the version.

float currentVersion = 7.0;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= currentVersion) {
        // iOS 7
        self.navBar.frame = CGRectMake(self.navBar.frame.origin.x, self.navBar.frame.origin.y, self.navBar.frame.size.width, 64);
    }

Disable Size classes and define custom autoresizing. Refer following images :-

 UITableView Autoresize:- 

自动调整UITableView的大小

 UISearchBar Autoresize:- 

UISearchBarController自动调整大小

NOTE: :- You can see above in size inspector, once you disable auto-layout and size classes.

在此处输入图片说明

Uncheck Adjust Scroll View Insets

I fixed it by changing the "top" auto layout anchor of the view that holds the segment controller from "super view" to "top layout guide". this moved the gap up to where the status bar is and changing the entire view's background color to the same color blue as the navbar made it seamless.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM