iOS 7 UISearchDisplayController search bar overlaps status bar while searching
Solution 1
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;
}];
}
}
Solution 2
Putting the following line in the viewDidLoad fixed it for me:
self.edgesForExtendedLayout = UIRectEdgeNone;
Solution 3
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;
}
Solution 4
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;
}
Solution 5
This seems to describe the problem I was having; hopefully it will help someone in my former positon.
Subclass your SearchDisplayController that's been added to your UIViewController/UITablewViewController,
Add something like this to its implementation:
- (void)setActive:(BOOL)visible animated:(BOOL)animated { [super setActive:visible animated:animated]; [self.searchContentsController.navigationController setNavigationBarHidden: NO animated: NO]; CGRect frame = self.searchResultsTableView.frame; frame.origin.y = CGRectGetHeight(self.searchContentsController.navigationController.navigationBar.frame); frame.size.height = CGRectGetHeight(frame) - CGRectGetMinY(frame); self.searchResultsTableView.frame = frame; frame = self.searchBar.frame; self.searchBar.frame = frame; [self.searchContentsController.view insertSubview:self.searchBar aboveSubview:self.searchResultsTableView]; }
desmondhume
Updated on July 08, 2022Comments
-
desmondhume almost 2 years
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:
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:
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!
-
Hackmodford almost 11 yearsThis and hodade's answer did not help me. It still automatically moves the searchbar up.
-
Hackmodford almost 11 yearsUpon further review this seems like a timing issue. Using DidBeginSearch will make it animate under the status bar then go back down some more...
-
smileyborg almost 11 yearsIf you're going this route,
self.edgesForExtendedLayout = UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight;
is probably a better and more general solution assuming your search bar is at the top of the screen. (This will preserve the extended layout for other edges, which is important for things like toolbars with blurring at the bottom of the screen.) -
Sandeep over 10 yearsWith out adding anything its working for me in iOS7 in some cases..but in one case its not working..what is the reason?why we need to write the above code?
-
richy over 10 yearsjust check [self respondsToSelector:@selector(edgesForExtendedLayout)] to prevent a crash on < iOS 7.
-
Jayprakash Dubey over 10 years@richy Didn't get your point. I'm getting error if same code is opened in Xcode 4.x.
-
richy over 10 yearsJayprakash XCode 4.x only supports iOS6.x and below so you will need to do
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]){self.edgesForExtendedLayout = UIRectEdgeNone;}
-
Megasaur over 10 yearsThis didn't work for me until I saw that view controllers have a reference for SearchDisplayControllers. Once I linked this in IB, it worked!
-
Deddiekoel over 10 yearsIt should, I've tested it in a UISplitView.
-
osxdirk about 10 yearsI suspect, this should go into a UISearchDisplayController subclass? This does not have a view property.
-
pronebird about 10 yearsOr actually set flag to adjust them for table view
-
Tunvir Rahman Tusher over 9 yearsThanks..this helps for me.
-
AlBirdie over 9 yearsThanks a lot @Salim! This solution works for a UISearchBar embedded in a UIViewController that is opened as a popup on iPad/iPhone6+.
-
Kanhaiya Sharma about 9 yearsself.edgesForExtendedLayout = UIRectEdgeNone; not works in my case thanks @tachiba
-
minerat about 9 yearsAwesome, this works on iOS7 (and 8) in simulator. Don't know why this isn't higher.
-
Vijay Masal over 7 yearsself.edgesForExtendedLayout = UIRectEdgeNone; work for me