在我的应用程序中,我实现了pull-to-refresh特性和自定义加载图标。在IPhone中,它有动态岛,它重叠了我的加载图标。
我想检测有无动态岛的设备。如果有,我会给它增加一些顶部空间。
发布于 2022-10-04 11:50:38
目前,据我所知,dynamic island将于2022年末包含在ActivityKit中。您可以从用于ActivityKit的此链接和苹果关于它的帖子查看。苹果公司也没有提供检查设备上是否有动态岛的方法。
但有一个办法可以让你得到你想要的东西。目前,动态岛仅在iPhone 14 Pro和iPhone 14 Pro Max上可用。所以只需要检查一下这两个设备。
Update:多亏了类型模型的此链接,iPhone 14 Pro和iPhone 14 Pro Max的名称模型类型是iPhone15,2和iPhone15,3,所以我们只需要检查这些情况。
代码会是这样的
extension UIDevice {
func checkIfHasDynamicIsland() -> Bool {
if let simulatorModelIdentifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] {
let nameSimulator = simulatorModelIdentifier
return nameSimulator == "iPhone15,2" || nameSimulator == "iPhone15,3" ? true : false
}
var sysinfo = utsname()
uname(&sysinfo) // ignore return value
let name = String(bytes: Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN)), encoding: .ascii)!.trimmingCharacters(in: .controlCharacters)
return name == "iPhone15,2" || name == "iPhone15,3" ? true : false
}
}用法
let value = UIDevice().checkIfHasDynamicIsland()
print("value: ", value)发布于 2022-11-02 01:35:57
safeAreaInsets值来检测动态岛。当设备方向为纵向时,safeAreaInsets.top等于59(显示缩放默认值)或51(显示缩放大文本)。用法:print(UIDevice.current.hasDynamicIsland)
extension UIDevice {
// Get this value after sceneDidBecomeActive
var hasDynamicIsland: Bool {
// 1. dynamicIsland only support iPhone
guard userInterfaceIdiom == .phone else {
return false
}
// 2. Get key window, working after sceneDidBecomeActive
guard let window = (UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.flatMap { $0.windows }.first { $0.isKeyWindow}) else {
print("Do not found key window")
return false
}
// 3.It works properly when the device orientation is portrait
return window.safeAreaInsets.top >= 51
}
}https://stackoverflow.com/questions/73946911
复制相似问题