1

在我的应用程序中,我使用了一个名为 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,如果有人能指出我在这里做错了什么以及如何访问 height 属性并对其进行修改,我将不胜感激?

4

2 回答 2

1

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

UIApplication.sharedApplication.statusBarFrame.size.height
于 2017-11-10T10:42:42.017 回答
-1

更改此行。

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

}
于 2017-11-10T11:22:29.223 回答