什么是等价的UI_USER_INTERFACE_IDIOM()在Swift检测之间的iPhone和iPad?
我得到一个使用未解决的标识符错误时,在Swift编译。
什么是等价的UI_USER_INTERFACE_IDIOM()在Swift检测之间的iPhone和iPad?
我得到一个使用未解决的标识符错误时,在Swift编译。
当前回答
试试这个检查当前设备是iPhone还是iPad:
斯威夫特5
struct Device {
static let IS_IPAD = UIDevice.current.userInterfaceIdiom == .pad
static let IS_IPHONE = UIDevice.current.userInterfaceIdiom == .phone
}
Use:
if(Device.IS_IPHONE){
// device is iPhone
}if(Device.IS_IPAD){
// device is iPad (or a Mac running under macOS Catalyst)
}else{
// other
}
其他回答
如果寇/换:
if UIDevice.current.userInterfaceIdiom == .pad {
// iPad
} else {
// not iPad (iPhone, mac, tv, carPlay, unspecified)
}
斯威夫特2. x:
加上别斯拉夫·图拉洛夫的回答,新的iPad Pro可以很容易地找到这一行
检测iPad Pro
struct DeviceType
{
...
static let IS_IPAD_PRO = UIDevice.currentDevice().userInterfaceIdiom == .Pad && ScreenSize.SCREEN_MAX_LENGTH == 1366.0
}
Swift 3(电视和汽车添加):
struct ScreenSize
{
static let SCREEN_WIDTH = UIScreen.main.bounds.size.width
static let SCREEN_HEIGHT = UIScreen.main.bounds.size.height
static let SCREEN_MAX_LENGTH = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
static let SCREEN_MIN_LENGTH = min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
}
struct DeviceType
{
static let IS_IPHONE = UIDevice.current.userInterfaceIdiom == .phone
static let IS_IPHONE_4_OR_LESS = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
static let IS_IPHONE_5 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0
static let IS_IPHONE_6 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0
static let IS_IPHONE_6P = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0
static let IS_IPHONE_7 = IS_IPHONE_6
static let IS_IPHONE_7P = IS_IPHONE_6P
static let IS_IPAD = UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH == 1024.0
static let IS_IPAD_PRO_9_7 = IS_IPAD
static let IS_IPAD_PRO_12_9 = UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH == 1366.0
static let IS_TV = UIDevice.current.userInterfaceIdiom == .tv
static let IS_CAR_PLAY = UIDevice.current.userInterfaceIdiom == .carPlay
}
struct Version{
static let SYS_VERSION_FLOAT = (UIDevice.current.systemVersion as NSString).floatValue
static let iOS7 = (Version.SYS_VERSION_FLOAT < 8.0 && Version.SYS_VERSION_FLOAT >= 7.0)
static let iOS8 = (Version.SYS_VERSION_FLOAT >= 8.0 && Version.SYS_VERSION_FLOAT < 9.0)
static let iOS9 = (Version.SYS_VERSION_FLOAT >= 9.0 && Version.SYS_VERSION_FLOAT < 10.0)
static let iOS10 = (Version.SYS_VERSION_FLOAT >= 10.0 && Version.SYS_VERSION_FLOAT < 11.0)
}
用法:
if DeviceType.IS_IPHONE_7P { print("iPhone 7 plus") }
if DeviceType.IS_IPAD_PRO_9_7 && Version.iOS10 { print("iPad pro 9.7 with iOS 10 version") }
当使用Swift时,你可以使用enum UIUserInterfaceIdiom,定义为:
enum UIUserInterfaceIdiom : Int {
case unspecified
case phone // iPhone and iPod touch style UI
case pad // iPad style UI (also includes macOS Catalyst)
}
所以你可以这样使用它:
UIDevice.current.userInterfaceIdiom == .pad
UIDevice.current.userInterfaceIdiom == .phone
UIDevice.current.userInterfaceIdiom == .unspecified
或者使用Switch语句:
switch UIDevice.current.userInterfaceIdiom {
case .phone:
// It's an iPhone
case .pad:
// It's an iPad (or macOS Catalyst)
@unknown default:
// Uh, oh! What could it be?
}
UI_USER_INTERFACE_IDIOM()是一个Objective-C宏,它被定义为:
#define UI_USER_INTERFACE_IDIOM() \ ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? \ [[UIDevice currentDevice] userInterfaceIdiom] : \ UIUserInterfaceIdiomPhone)
还要注意,即使在使用Objective-C时,UI_USER_INTERFACE_IDIOM()宏也只在针对iOS 3.2及以下时才需要。当部署到iOS 3.2及以上版本时,可以直接使用[UIDevice userInterfaceIdiom]。
供参考,我已经使用UI_USER_INTERFACE_IDIOM()为我的应用程序写在Swift。应用程序可以很好地使用XCode 6.3.1编译,没有任何警告,在模拟器(任何选择的设备)和我所有的真实设备(iPhone, iPad)上运行良好,iOS版本从7.1到8.3。
然而,这款应用在苹果评测者的设备上崩溃了(并被拒绝)。我花了几天时间才发现问题,并重新上传了几次到iTunes Connect。
现在我使用UIDevice.currentDevice()。取而代之的是userInterfaceIdiom,我的应用程序可以从这样的崩溃中幸存下来。
Swift 2.0 & iOS 9 & Xcode 7.1
// 1. request an UITraitCollection instance
let deviceIdiom = UIScreen.mainScreen().traitCollection.userInterfaceIdiom
// 2. check the idiom
switch (deviceIdiom) {
case .Pad:
print("iPad style UI")
case .Phone:
print("iPhone and iPod touch style UI")
case .TV:
print("tvOS style UI")
default:
print("Unspecified UI idiom")
}
Swift 3.0和Swift 4.0
// 1. request an UITraitCollection instance
let deviceIdiom = UIScreen.main.traitCollection.userInterfaceIdiom
// 2. check the idiom
switch (deviceIdiom) {
case .pad:
print("iPad style UI")
case .phone:
print("iPhone and iPod touch style UI")
case .tv:
print("tvOS style UI")
default:
print("Unspecified UI idiom")
}
使用UITraitCollection。 iOS trait环境是通过UITraitEnvironment协议的traitCollection属性公开的。以下类采用此协议:
UIScreen ui窗口 ui UIPresentationController UIView