根据设备是否为 iPhone X 更改状态栏高度

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

在我的应用程序中,我使用名为 MMDrawerController 的自定义 pod 来创建虚拟状态栏,不幸的是在 pod 中,状态栏的高度始终设置为 20

为了解决这个问题,我编写了以下代码:

应用委托

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)];

问题:

当我 运行 我的代码时 height 属性 总是 0,如果有人能指出我在这里做错了什么以及我将如何访问,我将不胜感激高度 属性 并修改它?

状态栏不要使用固定高度,您可以使用以下代码获取高度:

UIApplication.sharedApplication.statusBarFrame.size.height

更改此行。

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;
}

有了这个:

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");
}

}