iOS开发技巧xcode12和ios14

iPhone 12系列又要适配刘海屏了?

2020-10-23  本文已影响0人  夏天的枫_

现在iPhone系列的手机官方在售机型中,除iPhone SE 2全是是FaceID系列了(刘海屏),如今iPhone 12因为疫情原因,来的虽然比往年晚了点,但是它还是强势来袭,各大机构报告、评测层出不穷,又会有多少“真香”定律呢?
这,与本文无关,今天要看看iPhone 12系列是否又要迭代适配代码了呢?

判断长宽比方式

为了研究这个,前往APP预览规范 整理出以下表格

各iPhone长宽比
有谱了,那按这个就有了以下的方式去识别是否是FaceID的iPhone了
+ (BOOL)isFaceIDSeriecsiPhone
{
    if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
        NSLog(@"This not is iPhone! ");
        return NO; 
    }
    CGSize size = [UIScreen mainScreen].bounds.size;
    NSInteger value = size.width / size.height * 100;
    if (value == 216 || value ==  46) {
       return YES;
    }
    return NO;
}

虽然表格数值是217这是Excel四舍五入,但乘了100,再NSInteger取整的,取216即可。也不知道明年的iPhone是什么屏比,这个方式也不是一劳永逸,出了新的迭代再改,APP一年都不更新,那离下架也不远了(皮一下)。

判断底部安全区域

另一个方法是先判断系统版本,再获取UIWindow判断底部安全区域,形式,有些是获取
[UIApplication sharedApplication].delegate.window;[UIApplication sharedApplication].keyWindow;这个在iOS 13之后不可用了。那就从windows

+ (BOOL)isFaceIDSeriecsiPhone
{
    if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
        NSLog(@"This not is iPhone! ");
        return NO; 
    }
    if (@available(iOS 11.0, *)) {
        UIWindow * window =  [self mainWindow];
        // 判断安全区域方式
        if (window.safeAreaInsets.bottom > 0.0) {
            return YES;
        }
    }
    return NO;
}
+(UIWindow *)mainWindow{
    id appDelegate = [UIApplication sharedApplication].delegate;
    if (appDelegate && [appDelegate respondsToSelector:@selector(window)]) {
        return [appDelegate window];
    }
    NSArray *windows = [UIApplication sharedApplication].windows;
    if ([windows count] == 1) {
        return [windows firstObject];
    } else {
        for (UIWindow *window in windows) {
            if (window.windowLevel == UIWindowLevelNormal) {
                return window;
            }
        }
    }
    return nil;
}

safeAreaInsets
竖屏:UIEdgeInsets -> (top = 44, left = 0, bottom = 34, right = 0)
横屏:UIEdgeInsets -> (top = 0, left = 44, bottom = 21, right = 44)

上一篇 下一篇

猜你喜欢

热点阅读