Created
July 29, 2020 07:53
-
-
Save michzio/c7af02363be9599e125f45f3a4506694 to your computer and use it in GitHub Desktop.
DeviceType
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
struct ScreenSize | |
{ | |
static var SCREEN_WIDTH : CGFloat { UIScreen.main.bounds.size.width } | |
static var SCREEN_HEIGHT : CGFloat { UIScreen.main.bounds.size.height } | |
static var SCREEN_MAX_LENGTH : CGFloat { max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT) } | |
static var SCREEN_MIN_LENGTH : CGFloat { min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT) } | |
} | |
struct DeviceFeatures { | |
static var IS_LANDSCAPE : Bool { | |
ScreenSize.SCREEN_WIDTH > ScreenSize.SCREEN_HEIGHT | |
// or | |
// UIApplication.shared.statusBarOrientation.isLandscape | |
} | |
static var IS_PORTRAIT: Bool { | |
ScreenSize.SCREEN_HEIGHT > ScreenSize.SCREEN_WIDTH | |
// or | |
//UIApplication.shared.statusBarOrientation.isPortrait | |
} | |
/** | |
* This property tries to get 4 orientations: | |
* landscapeLeft, landscapeRight, portrait, portraitUpsideDown | |
* as defined by UIDeviceOrientation enum. | |
* It can return also unknown orientation (which is rare case) | |
*/ | |
static var ORIENTATION: UIDeviceOrientation { | |
// if device orientation is one of the portrait or landscape | |
// return just this orinentation | |
let deviceOrientation = UIDevice.current.orientation | |
if deviceOrientation.isPortrait || deviceOrientation.isLandscape { | |
return deviceOrientation | |
} | |
// otherwise if there is flat (faceUp, faceDown), unknown orientation | |
// try to get orientation from UIInterfaceOrientation | |
// Notice that: | |
// UIDeviceOrientationLandscapeRights => UIInterfaceOrientationLandscapeLeft | |
// UIDeviceOrientationLandscapeLeft => UIInterfaceOrientationLandscapeRight | |
let interfaceOrientation = UIApplication.shared.statusBarOrientation | |
switch interfaceOrientation { | |
case .landscapeLeft: | |
return UIDeviceOrientation.landscapeRight | |
case .landscapeRight: | |
return UIDeviceOrientation.landscapeLeft | |
case .portrait: | |
return .portrait | |
case .portraitUpsideDown: | |
return .portraitUpsideDown | |
case .unknown: | |
break | |
} | |
// otherwise get orientation based on screen size | |
if DeviceFeatures.IS_PORTRAIT { | |
return .portrait | |
} else if DeviceFeatures.IS_LANDSCAPE { | |
return .landscapeLeft | |
} | |
return .unknown | |
} | |
static var INTERFACE_ORIENTATION: UIInterfaceOrientation { | |
return UIApplication.shared.statusBarOrientation | |
} | |
} | |
struct DeviceType | |
{ | |
static var IS_ANY_IPHONE: Bool { | |
UIDevice.current.userInterfaceIdiom == .phone | |
} | |
static var IS_SMALL : Bool { | |
UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH < 600.0 | |
} | |
static var IS_MEDIUM : Bool { | |
UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH < 700.0 | |
} | |
static var IS_IPHONE_4_OR_LESS : Bool { UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0 } | |
static var IS_IPHONE_5_5S_SE : Bool { UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0 } | |
static var IS_IPHONE_6_7_8 : Bool { UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0 } | |
static var IS_IPHONE_6P_7P_8P : Bool { UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0 } | |
static var IS_IPHONE_X_XS : Bool { UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 812.0 } | |
static var IS_IPHONE_XR_XS_MAX : Bool { UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 896.0 } | |
static var IS_IPAD : Bool { UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH == 1024.0 } | |
static var IS_IPAD_PRO : Bool { UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH == 1366.0 } | |
static var IS_ANY_IPAD : Bool { UIDevice.current.userInterfaceIdiom == .pad } | |
static var HAS_TOP_NOTCH : Bool { | |
// with notch: 44.0 on iPhone X, XS, XS Max, XR. | |
// without notch: 24.0 on iPad Pro 12.9" 3rd generation, 20.0 on iPhone 8 on iOS 12+. | |
UIApplication.shared.windows.first?.safeAreaInsets.top ?? 0 > 24 | |
} | |
static var SAFE_AREA : UIEdgeInsets { | |
UIApplication.shared.windows.first?.safeAreaInsets ?? .zero | |
} | |
static var HAS_BOTTOM_SAFE_AREA : Bool { | |
// with home indicator: 34.0 on iPhone X, XS, XS Max, XR. | |
// with home indicator: 20.0 on iPad Pro 12.9" 3rd generation. | |
UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 0 > 0 | |
} | |
} | |
extension DeviceType { | |
static var IS_IPHONE_PORTRAIT: Bool { | |
UIDevice.current.userInterfaceIdiom == .phone && DeviceFeatures.IS_PORTRAIT | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment