IOS 判断iPhone刘海屏

本文介绍了一个用于判断iPhone是否为刘海屏并获取刘海高度的工具类。该工具类适用于iOS11及以上版本,通过检查设备屏幕的安全区域来确定刘海的存在及高度。
部署运行你感兴趣的模型镜像

现在Apple市场上,越来越流行刘海屏手机(与Android市场一样,往刘海屏手机方向发展趋势)。

在iPhone手机角度上看,刘海屏出现在机型较新的手机上(相对较旧/老的iPhone上还没出现)。

那么,如何判断当前的iPhone手机是刘海屏手机呢?同时,如何获取刘海高度?

在这里,提供一个工具类:

NotchScreenUtil.h

/*
 * iPhone刘海屏工具类
 */
@interface NotchScreenUtil : NSObject

// 判断是否是刘海屏
+(BOOL)isIPhoneNotchScreen;

// 获取刘海屏高度
+(CGFloat)getIPhoneNotchScreenHeight;

@end

NotchScreenUtil.m

#import <Foundation/Foundation.h>
#import "NotchScreenUtil.h"

@implementation NotchScreenUtil

+ (BOOL)isIPhoneNotchScreen{
    BOOL result = NO;
    if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
        return result;
    }
    if (@available(iOS 11.0, *)) {
        CGSize size = [UIScreen mainScreen].bounds.size;
        NSInteger notchValue = size.width / size.height * 100;
        UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
        if (mainWindow.safeAreaInsets.bottom > 0.0 || notchValue == 216 || notchValue == 46) {
            result = YES;
        }
    }
    return result;
}

+ (CGFloat)getIPhoneNotchScreenHeight{
    /*
     * iPhone8 Plus  UIEdgeInsets: {20, 0, 0, 0}
     * iPhone8       UIEdgeInsets: {20, 0, 0, 0}
     * iPhone XR     UIEdgeInsets: {44, 0, 34, 0}
     * iPhone XS     UIEdgeInsets: {44, 0, 34, 0}
     * iPhone XS Max UIEdgeInsets: {44, 0, 34, 0}
     */
    CGFloat bottomSpace = 0;
    UIEdgeInsets safeAreaInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    if (@available(iOS 11.0, *)) {
        if (@available(iOS 13.0, *)) {
            safeAreaInsets = [UIApplication sharedApplication].windows.firstObject.safeAreaInsets;
        } else {
            safeAreaInsets = [[UIApplication sharedApplication] delegate].window.safeAreaInsets;
        }
        switch (UIApplication.sharedApplication.statusBarOrientation) {
            case UIInterfaceOrientationPortrait:
            {
                bottomSpace = safeAreaInsets.bottom;
            }
                break;
            case UIInterfaceOrientationLandscapeLeft:
            {
                bottomSpace = safeAreaInsets.right;
            }
                break;
            case UIInterfaceOrientationLandscapeRight:
            {
                bottomSpace = safeAreaInsets.left;
            }
                break;
            case UIInterfaceOrientationPortraitUpsideDown:
            {
                bottomSpace = safeAreaInsets.top;
            }
                break;
            default:
            {
                bottomSpace = safeAreaInsets.bottom;
            }
                break;
        }
    }
    return bottomSpace;
}

@end

注:开放接口可用范围在iOS 11以上。

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值