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

如何在SwiftUI中检测macOS环境下Studio Display的屏幕尺寸

Studio Display 检测及适配方案

你遇到的尺寸差异是因为Studio Display的默认点分辨率和MacBook Pro内置屏幕的缩放逻辑不同,不需要修改iPadOS侧的适配逻辑,只需要在macOS端根据屏幕类型调整窗口默认大小即可

检测原理

苹果官方硬件都有固定的厂商ID和产品ID,直接读取屏幕的硬件标识即可精准判断是否为Studio Display,无需依赖系统版本、屏幕分辨率等易变参数。

具体实现方法

  • 获取当前所有连接屏幕的NSScreen实例,读取每个实例的deviceDescription属性
  • 从属性中提取NSScreenNumber字段,转换为CGDirectDisplayID类型
  • 调用CGDisplayVendorNumber获取厂商ID,苹果的厂商ID固定为0x610
  • 调用CGDisplayProductNumber获取产品ID,Studio Display的有效产品ID为0xAE62、0xAE63,覆盖所有在售配置和固件版本
  • 两个ID都匹配即可判定当前屏幕为Studio Display

Swift 示例代码

import AppKit

func isRunningOnStudioDisplay() -> Bool {
    let appleVendorID: UInt32 = 0x610
    // 覆盖所有Studio Display的产品ID
    let studioDisplayProductIDs: Set<UInt32> = [0xAE62, 0xAE63]
    
    for screen in NSScreen.screens {
        guard let screenNum = screen.deviceDescription[NSDeviceDescriptionKey(rawValue: "NSScreenNumber")] as? NSNumber else {
            continue
        }
        let displayID = CGDirectDisplayID(screenNum.intValue)
        let vendorID = CGDisplayVendorNumber(displayID)
        let productID = CGDisplayProductNumber(displayID)
        
        if vendorID == appleVendorID, studioDisplayProductIDs.contains(productID) {
            return true
        }
    }
    return false
}

适配建议

检测到Studio Display后,优先调整窗口的尺寸约束比例,不要硬编码固定像素值,避免后续苹果推出新硬件或更新固件时适配失效。如果你的应用支持多屏幕拖动,建议监听NSWindow.didChangeScreenNotification通知,在窗口切换屏幕时动态调整尺寸。


内容的提问来源于stack exchange,提问作者Antonio A. Chavez

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 03:09:05