Change status bar height, based on if the device is an iPhone X or not

725 Views Asked by At

In my application, I use a custom pod called MMDrawerController, to create a dummy status bar, unfortunately in the pod the status bar's height is always set to 20.

In order to fix this issue I have written the following code:

App delegate

MMDrawerController *mmdrawer = [[MMDrawerController alloc]init];


//UPDATE IPHONE X STATUS BAR
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    if (screenSize.height == 812.0f) {
        NSLog(@"DEVICE NAME : iPhone X");
        self.iphoneXHeight = -45.0;
        self.iphoneXHeightPos = 45.0;
        mmdrawer.height = 40;
    }
    else {

        self.iphoneXHeight = -20.0;
        self.iphoneXHeightPos = 20.0;
        mmdrawer.height = 20;


    }
}

MMDrawerController.h

@property (nonatomic,assign) CGFloat height;

MMDrawerController.m

  _dummyStatusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), self.height)];

Problem:

When I run my code the height property is always 0, would appreciate it if someone can point out what I am doing wrong here and how would I be able access the height property and modify it ?

2

There are 2 best solutions below

0
Tj3n On BEST ANSWER

Dont use fixed height for the status bar, you can get the height with this code:

UIApplication.sharedApplication.statusBarFrame.size.height
0
Mukesh On

Change this line.

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if (screenSize.height == 812.0f) {
    NSLog(@"DEVICE NAME : iPhone X");
    self.iphoneXHeight = -45.0;
    self.iphoneXHeightPos = 45.0;
    mmdrawer.height = 40;
}

With this one:

if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone) {
  // determine screen size
int screenHeight = [UIScreen mainScreen].bounds.size.height;

switch (screenHeight) {
        // iPhone 5s
    case 568:
        NSLog(@"iPhone 5 or 5S or 5C");
        break;
        // iPhone 6
    case 667:
        NSLog(@"iPhone 6/6S/7/8");
        break;
        // iPhone 6 Plus
    case 736:
        NSLog(@"iPhone 6+/6S+/7+/8+");
        break;
        // iPhone X
    case 814:
        NSLog(@"iPhone X");
        break;
    default:
        // it's an iPad
        printf("unknown");
}

}