SwiftでUI_を使うUSER_INTERFACE_IDIOM()現在のデバイスの表示
1196 ワード
Swiftで
Swiftでは、
次のように使用できます.
またはSwitch文を使用します.
また、Objective-Cを記述する場合、
UI_USER_INTERFACE_IDIOM()
と同等で、デバイスがiPhoneなのかiPadなのかを検出する方法は何ですか?Swiftでは、
enum
UIUserInterfaceIdiom
を使用できます.定義は次のとおりです.enum UIUserInterfaceIdiom : Int {
case unspecified
case phone // iPhone iPod touch UI
case pad // iPad UI
}
次のように使用できます.
UIDevice.current.userInterfaceIdiom == .pad
UIDevice.current.userInterfaceIdiom == .phone
UIDevice.current.userInterfaceIdiom == .unspecified
またはSwitch文を使用します.
switch UIDevice.current.userInterfaceIdiom {
case .phone:
// iPhone
case .pad:
// iPad
case .unspecified:
// Uh, oh! ?
}
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]
をそのまま使えばいいです.