Back in Objective-C, I defined the following macros in a constants.h
file:
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?
For IPad :
static let IS_IPAD = UIDevice.currentDevice().userInterfaceIdiom == .Pad && ScreenSize.SCREEN_MAX_LENGTH >= 1024.0
I tend to use these three structs in every project i do:
struct ScreenSize {
static let SCREEN_WIDTH = UIScreen.mainScreen().bounds.size.width
static let SCREEN_HEIGHT = UIScreen.mainScreen().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_4_OR_LESS = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
static let IS_IPHONE_5 = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0
static let IS_IPHONE_6 = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0
static let IS_IPHONE_6P = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0
static let IS_IPAD = UIDevice.currentDevice().userInterfaceIdiom == .Pad && ScreenSize.SCREEN_MAX_LENGTH == 1024.0
}
struct iOSVersion {
static let SYS_VERSION_FLOAT = (UIDevice.currentDevice().systemVersion as NSString).floatValue
static let iOS7 = (iOSVersion.SYS_VERSION_FLOAT < 8.0 && iOSVersion.SYS_VERSION_FLOAT >= 7.0)
static let iOS8 = (iOSVersion.SYS_VERSION_FLOAT >= 8.0 && iOSVersion.SYS_VERSION_FLOAT < 9.0)
static let iOS9 = (iOSVersion.SYS_VERSION_FLOAT >= 9.0 && iOSVersion.SYS_VERSION_FLOAT < 10.0)
}
And i use them in the following manner:
if DeviceType.IS_IPHONE_6 {
//iPhone 6 specific code goes here.
}
In this case, the easiest way to define these constants seems to be to use the let
keyword. Macros are not available in Swift, so you have to use constants in this case (which may be better in terms of performance anyhow, in this case):
let IS_IPHONE5 = fabs(UIScreen.mainScreen().bounds.size.height-568) < 1;
let IS_IPAD = (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad);
let IS_IOS8 = (UIDevice.currentDevice().systemVersion.floatValue >= 8)
let APP_DEFAULT_FONT_FACE_NAME = "HelveticaNeue-Light";
let APP_DEFAULT_FONT_FACE_THIN_NAME = "HelveticaNeue-UltraLight";
let APP_VERSION_STRING = "1.2";
for the IS_IPHONE5
constant, I used fabs
to avoid possible floating point rounding errors. I'm not sure if they would be an issue, but it's better to be safe...
The IS_IOS7
macro is useless in Swift, because Swift only supports iOS 7 and later versions (IS_IOS7
would always be YES
in a Swift program.) I added an IS_IOS8
constant instead...