Skip to content

Instantly share code, notes, and snippets.

@reeichert
Created January 3, 2018 17:10
Show Gist options
  • Save reeichert/d642c36f6721e9b969fd6e5b752a8fc2 to your computer and use it in GitHub Desktop.
Save reeichert/d642c36f6721e9b969fd6e5b752a8fc2 to your computer and use it in GitHub Desktop.
Screen sizes
import UIKit
public struct Screen {
/// Retrieves the device bounds.
public static var bounds: CGRect {
return UIScreen.main.bounds
}
/// Retrieves the device width.
public static var width: CGFloat {
return bounds.width
}
/// Retrieves the device height.
public static var height: CGFloat {
return bounds.height
}
/// Retrieves the device scale.
public static var scale: CGFloat {
return UIScreen.main.scale
}
static func size() -> Size {
let w: Double = Double(UIScreen.main.bounds.width)
let h: Double = Double(UIScreen.main.bounds.height)
let screenHeight: Double = max(w, h)
switch screenHeight {
case 480:
return .screen3_5Inch
case 568:
return .screen4Inch
case 667:
return UIScreen.main.scale == 3.0 ? .screen5_5Inch : .screen4_7Inch
case 736:
return .screen5_5Inch
case 812:
return .screen5_8Inch
default:
return .unknownSize
}
}
}
/// Get Size of device
///
/// - unknownSize: unknownSize description
/// - screen3_5Inch: iPhone 4/4S
/// - screen4Inch: iPhone 5/5S/SE
/// - screen4_7Inch: iPhone 6/6S/7/7S
/// - screen5_5Inch: iPhone Plus
public enum Size: Int {
case unknownSize = 0
/// iPhone 4 / 4S
case screen3_5Inch
/// iPhone 5 / 5S / SE
case screen4Inch
/// iPhone 6 / 7
case screen4_7Inch
/// iPhone 6Plus / 7Plus
case screen5_5Inch
/// iPhone X
case screen5_8Inch
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment