Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix iOS 7 status bar height and width issue

I have made a custom toast library (very simple, just a box which appears under navigation bar). Which works fine in iOS 6 which is my target group. But since iOS 7 has released it didn't display correctly.

The way I tried to fix it was through this code:

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7,0")){
    self.offset = [UIApplication sharedApplication].statusBarFrame.size.height + self.viewController.navigationController.navigationBar.frame.size.height;
}

I made a macro which can be found on Stack overflow to detect if the iOS version is 7 and then add the status bar height and the navigation height. This works correctly in portrait mode in iOS 7, but when I switch to landscape offset becomes 512?.

Can anybody explain way this happens and how I can fix this?


Decided to split the code because a comment and see what is exactly causing the difference. What I did was:

CGFloat statusBarHeight =[UIApplication sharedApplication].statusBarFrame.size.height;

CGFloat navBar = self.viewController.navigationController.navigationBar.frame.size.height;
self.offset = statusBarHeight + navBar;
NSLog(@"Init - statusBarHeight: %f, navBar: %f", statusBarHeight, navBar);

As it turns out for some reason statusbar is in portrait 20 and in landscape it is 480


This is because height and width are turned around, answer came from comment

like image 300
Haagenti Avatar asked Oct 23 '13 06:10

Haagenti


People also ask

Where is the status bar on iPhone?

The icons in the status bar at the top of the screen provide information about iPhone. On an iPhone with Face ID, there are additional status icons at the top of Control Center.

How do I get the status bar height in react native?

"React-native-status-bar-height" is a small library that helps you to get status bar height. Save this answer. Show activity on this post. For the situation where I need to escape a StatusBar component, my solution on Android is to use StatusBar.


2 Answers

As I have previously done same "fixes", I've noticed that in landscape

[UIApplication sharedApplication].statusBarFrame.size.width  

and

 [UIApplication sharedApplication].statusBarFrame.size.height  

values are switched.

like image 118
Guntis Treulands Avatar answered Nov 03 '22 00:11

Guntis Treulands


Travis Jeffery provided an excellent explanation on http://travisjeffery.com/b/2013/05/using-the-status-bars-frame-in-ios/

Let me copy it from his site:

"The trick to the status bar’s frame and height is to make sure you’ve converted it to the coordinate space of the view that you care about, this will probably be a UIViewController’s view or subview."

CGRect statusBarFrame = [yourView.window convertRect:UIApplication.sharedApplication.statusBarFrame toView:yourView];
CGFloat statusBarHeight = statusBarFrame.size.height;
like image 31
Golden Thumb Avatar answered Nov 02 '22 23:11

Golden Thumb