Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7 UISearchDisplayController search bar overlaps status bar while searching

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

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

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!

like image 475
desmondhume Avatar asked Sep 20 '13 21:09

desmondhume


4 Answers

Putting the following line in the viewDidLoad fixed it for me:

self.edgesForExtendedLayout = UIRectEdgeNone;
like image 73
Deddiekoel Avatar answered Nov 16 '22 09:11

Deddiekoel


Thank you hodade for leading me on the right track! Your solution worked, except it only moved the search bar's frame, leaving my other subviews in the wrong spot. The only thing I changed was to move all the subviews in my view, as well as animate it.

Thanks!

-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        CGRect statusBarFrame =  [[UIApplication sharedApplication] statusBarFrame];
        [UIView animateWithDuration:0.25 animations:^{
            for (UIView *subview in self.view.subviews)
                subview.transform = CGAffineTransformMakeTranslation(0, statusBarFrame.size.height);
        }];
    }
}

-(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        [UIView animateWithDuration:0.25 animations:^{
            for (UIView *subview in self.view.subviews)
                subview.transform = CGAffineTransformIdentity;
        }];
    }
}
like image 39
desmondhume Avatar answered Nov 16 '22 10:11

desmondhume


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;
}
like image 13
tachiba Avatar answered Nov 16 '22 09:11

tachiba


Just place following code in -(void) ViewDidLoad. It will work for iOS 7 and later version

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

UPDATE:

if(SYSTEM_VERSION_GREATER_THAN(@"6.1")) {
    self.edgesForExtendedLayout = UIRectEdgeNone;
}
like image 7
MaxEcho Avatar answered Nov 16 '22 08:11

MaxEcho