Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get iPhone Status Bar Height

I need to resize some elements in relation to the height of the iPhone's Status Bar. I know that the status bar is usually 20 points high but this isn't the case when it's in tethering mode. It gets doubled to 40. What is the proper way to determine it's height? I've tried

[[UIApplication sharedApplication] statusBarFrame]

but it gives me 20 x 480 in landscape which is correct but then it gives me 320 x 40 in portrait. Why isn't it giving me the opposite of that (40 x 320)?

like image 321
Kyle Decot Avatar asked Oct 08 '10 07:10

Kyle Decot


People also ask

How do I change the size of the notification bar on my iPhone?

To enable this you'll want to go to Settings > Accessibility > Display & Text Size, and turn on the Larger Text option. You'll want to move the text size slider to 55% or higher (if it is set at the middle of the slider bar or any lower this will not work), then you can tap on the status bar icons to enlarge them.

What is the status bar on iPhone?

A status bar appears along the upper edge of the screen and displays information about the device's current state, like the time, cellular carrier, and battery level.


3 Answers

The statusBarFrame returns the frame in screen coordinates. I believe the correct way to get what this corresponds to in view coordinates is to do the following:

- (CGRect)statusBarFrameViewRect:(UIView*)view 
{
    CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];

    CGRect statusBarWindowRect = [view.window convertRect:statusBarFrame fromWindow: nil];

    CGRect statusBarViewRect = [view convertRect:statusBarWindowRect fromView: nil];

    return statusBarViewRect;
}

Now in most cases the window uses the same coordinates as the screen, so [UIWindow convertRect:fromWindow:] doesn't change anything, but in the case that they might be different this method should do the right thing.

like image 171
ThomasW Avatar answered Oct 17 '22 04:10

ThomasW


Did you do it like this:

CGRect rect;

rect = [[UIApplication sharedApplication] statusBarFrame];
NSLog(@"Statusbar frame: %1.0f, %1.0f, %1.0f, %1.0f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
like image 22
o15a3d4l11s2 Avatar answered Oct 17 '22 04:10

o15a3d4l11s2


EDIT The iOS 11 way to work out where to put the top of your view content is UIView's safeAreaLayoutGuide See UIView Documentation.

DEPRECATED ANSWER If you're targeting iOS 7+, The documentation for UIViewController advises that the viewController's topLayoutGuide property gives you the bottom of the status bar, or the bottom of the navigation bar, if it's also visible. That may be of use, and is certainly less hack than many of the previous solutions.

like image 7
Joshua J. McKinnon Avatar answered Oct 17 '22 05:10

Joshua J. McKinnon