iOS 7 UISearchDisplayController search bar overlap

2019-01-10 01:48发布

I'm updating my app for iOS 7, and I'm in the process of adjusting all my views to account for the new transparent status bar (my app will still use opaque navigation bars).

It was relatively easy to adjust for the status bar in every view, except one major problem I'm having with a UISearchBar connected to a UISearchDisplayController in one of my view controllers.

The search bar seems to display normally, as shown below:

Search Bar http://imageshack.us/a/img163/9128/06vx.png

The problem is, as soon as I begin searching, the navigation bar disappears (as it should), but everything else also moves up to overlap the status bar:

Broken Search Bar http://imageshack.us/a/img11/8237/corh.png

This doesn't appear to be working as intended, since the darkening of the screen happens 20 pixels below the search bar, where the search bar should end.

Is there a built in solution for this in iOS 7? I'd rather not have to manually adjust the frame for every view each time the user begins and ends searching.

Thanks!

12条回答
乱世女痞
2楼-- · 2019-01-10 01:58
-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        CGRect statusBarFrame =  [[UIApplication sharedApplication] statusBarFrame];
        CGRect frame = controller.searchBar.frame;
        frame.origin.y += statusBarFrame.size.height;
        controller.searchBar.frame = frame;
    }
}

-(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        CGRect statusBarFrame =  [[UIApplication sharedApplication] statusBarFrame];
        CGRect frame = controller.searchBar.frame;
        frame.origin.y -= statusBarFrame.size.height;
        controller.searchBar.frame = frame;
    }
}
查看更多
Fickle 薄情
3楼-- · 2019-01-10 01:59

You're may using no translucent navigation bar? If so, this will solve it.

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    self.navigationController.navigationBar.translucent = YES;
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    self.navigationController.navigationBar.translucent = NO;
}
查看更多
可以哭但决不认输i
4楼-- · 2019-01-10 02:00

I think maybe add this to viewDidLoad will help:

if([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
{
    self.edgesForExtendedLayout = UIRectEdgeNone;

}
查看更多
我只想做你的唯一
5楼-- · 2019-01-10 02:00

just add

self.definesPresentationContext = YES;

you can read more from here: UISearchController and definesPresentationContext

and from Apple documentation: UISearchController documentation

Note: UISearchDispalyController is deprecated in iOS7, use UISearchController instead in iOS8, method above use UISearchController

查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-10 02:03

In my case, the views below the search bar were at their right place, only the search bar was overlapping the status bar. In this case, this peace of code works fine:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {        
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        CGRect statusBarFrame =  [[UIApplication sharedApplication] statusBarFrame];
        CGRect frame = self.searchBar.frame;
        frame.origin.y += statusBarFrame.size.height;
        self.searchBar.frame = frame;
    }
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        CGRect statusBarFrame =  [[UIApplication sharedApplication] statusBarFrame];
        CGRect frame = self.searchBar.frame;
        frame.origin.y -= statusBarFrame.size.height;
        self.searchBar.frame = frame;
    }
}

Hope it will be useful to others

查看更多
女痞
7楼-- · 2019-01-10 02:03

Subclass your SearchDisplayController that's been added to your UIViewController/UITablewViewController and add this to its implementation -

- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
    if(self.active == visible) return;
    [self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO];
    [super setActive:visible animated:animated];
    [self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];
    if (visible) {
        [self.searchBar becomeFirstResponder];
    } else {
        [self.searchBar resignFirstResponder];
    }
}
查看更多
登录 后发表回答