UISearchBar overlaps status bar in iOS

Fotis Paraskevopoulos picture Fotis Paraskevopoulos · Sep 24, 2013 · Viewed 16.9k times · Source

I (just like everyone else here) am running into the same Status Bar overlap issue that everyone else is, with a little twist, and that is why I opening a new question about this.

There seems to be some mechanism that lets the UISearchBar know where to position it self, which is totally out of whack.

jaredsinclair answer here (iOS 7 status bar back to iOS 6 default style in iPhone app?) , explains in great detail how the Apple Engineers allow us to introduce logic to our application in order to blend in as much as possible with the user's environment.

I went through the process of carefully examining each UIViewController in my application and making the slightest modification possible.

In most cases I was able to resolve the problem using the code below which I found accross several SO answers

// Do any additional setup after loading the view.
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]){
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

This however would not work no matter what I do in a particular view where the UINavigationBar is hidden.

UISearchBar on top of status bar

Based on the solution found across SO I was able to solve this issue by adding the following piece of logic to this particular UIViewController

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
    CGRect statusBarFrame =  [[UIApplication sharedApplication] statusBarFrame];
    viewFrame.origin.y = viewFrame.origin.y+statusBarFrame.size.height;
}

This pushes the UIViewController n pixels down "depending" on the height of the status bar.

The effect of this is show here

Hacked position UISearchBar

The problem is that when I enter the search field the UISearchBar adds by itself a 20px padding on the top, which offsets the entire UI.

enter image description here

This leads me to the conclusion that the UISearchBar tries to adjust itself, and by coincidence it adjust itself the exact same amount as the status bar's height.

If I don't hack the position, once I enter the search field then this auto adjustment aligns the UISearchBar neatly underneath the status bar.

I hope I have detailed my confusion, I am wondering if anybody has any ideas towards a solution.

Answer

Sunil Adhyaru picture Sunil Adhyaru · Oct 4, 2013

If the navigationBar is visible do the following in ViewDidLoad :

self.edgesForExtendedLayout = UIRectEdgeNone;

If the navigationBar is hidden do the following in ViewDidLoad :

Adjust all the UIView elements by shifting them 20pt