You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS/Swift:获取屏幕圆角半径与刘海坐标(绘制边框用)

获取屏幕圆角半径与刘海区域信息的方法

iOS平台

1. 屏幕圆角半径获取

  • iOS 16及以上有公开API直接获取:
if #available(iOS 16.0, *) {
    let cornerRadius = UIScreen.main.displayCornerRadius
    // 拿到的是实际圆角半径(单位:pt)
}
  • 旧版本系统无公开API,可通过安全区域估算(仅供参考):
let screenBounds = UIScreen.main.bounds
let safeAreaInsets = UIApplication.shared.windows.first?.safeAreaInsets ?? .zero
// 圆角半径约等于屏幕左右安全区域的缩进值
let estimatedRadius = safeAreaInsets.left // 左右缩进值通常相等

2. 刘海区域坐标获取

通过安全区域和状态栏高度判断刘海存在性,再计算区域范围:

let window = UIApplication.shared.windows.first
let safeAreaInsets = window?.safeAreaInsets ?? .zero
let statusBarHeight = UIApplication.shared.statusBarFrame.height

let hasNotch = safeAreaInsets.top > statusBarHeight
if hasNotch {
    // 刘海区域为屏幕顶部从y=0到y=safeAreaInsets.top的整宽区域
    let notchFrame = CGRect(x: 0, y: 0, width: window?.bounds.width ?? 0, height: safeAreaInsets.top)
    // notchFrame即为刘海的坐标边界
}

Android平台

1. 屏幕圆角半径获取

  • Android 11(API 30)及以上支持直接获取各角圆角半径:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    // 获取左上角圆角半径,替换POSITION可获取其他角
    val topLeftRadius = window.insets.getRoundedCorner(WindowInsets.RoundedCorner.POSITION_TOP_LEFT)?.radius
    // 其他角可选值:POSITION_TOP_RIGHT、POSITION_BOTTOM_LEFT、POSITION_BOTTOM_RIGHT
}
  • 旧版本无公开API,可通过读取系统资源或自定义维度值估算,部分设备可通过反射读取系统属性(注意兼容性)。

2. 刘海区域坐标获取

Android 9(API 28)及以上可通过DisplayCutout获取刘海区域:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    window.decorView.rootWindowInsets.displayCutout?.let { cutout ->
        // 刘海区域封装在boundingRects列表中(通常仅一个)
        cutout.boundingRects.forEach { rect ->
            // rect.left/top/right/bottom即为刘海的坐标边界
        }
    }
}

内容的提问来源于stack exchange,提问作者g123k

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 12:39:51