Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Display mode
import UIKit
public enum DisplayType {
case unknown
case iphone4
case iphone5
case iphone6
case iphone6plus
static let iphone7 = iphone6
static let iphone7plus = iphone6plus
case iphoneX
}
public final class Display {
class var width:CGFloat { return UIScreen.main.bounds.size.width }
class var height:CGFloat { return UIScreen.main.bounds.size.height }
class var maxLength:CGFloat { return max(width, height) }
class var minLength:CGFloat { return min(width, height) }
class var zoomed:Bool { return UIScreen.main.nativeScale >= UIScreen.main.scale }
class var retina:Bool { return UIScreen.main.scale >= 2.0 }
class var phone:Bool { return UIDevice.current.userInterfaceIdiom == .phone }
class var pad:Bool { return UIDevice.current.userInterfaceIdiom == .pad }
class var carplay:Bool { return UIDevice.current.userInterfaceIdiom == .carPlay }
class var tv:Bool { return UIDevice.current.userInterfaceIdiom == .tv }
class var typeIsLike:DisplayType {
if phone && maxLength < 568 {
return .iphone4
}
else if phone && maxLength == 568 {
return .iphone5
}
else if phone && maxLength == 667 {
return .iphone6
}
else if phone && maxLength == 736 {
return .iphone6plus
}
else if phone && maxLength == 812 {
return .iphoneX
}
return .unknown
}
}
print("width: \(Display.width)")
print("height: \(Display.height)")
print("maxLength: \(Display.maxLength)")
print("minLength: \(Display.minLength)")
print("zoomed: \(Display.zoomed)")
print("retina: \(Display.retina)")
print("phone: \(Display.phone)")
print("pad: \(Display.pad)")
print("carplay: \(Display.carplay)")
print("tv: \(Display.tv)")
print("Type is like: \(Display.typeIsLike)")
// iphone 6s without launch screen
width: 320.0
height: 480.0
maxLength: 480.0
minLength: 320.0
zoomed: false
retina: true
phone: true
pad: false
carplay: false
tv: false
Type is like: iphone4
// iphone SE portrait
width: 320.0
height: 568.0
maxLength: 568.0
minLength: 320.0
zoomed: true
retina: true
phone: true
pad: false
carplay: false
tv: false
Type is like: iphone5
// iphone 7 plus portrait
width: 414.0
height: 736.0
maxLength: 736.0
minLength: 414.0
zoomed: true
retina: true
phone: true
pad: false
carplay: false
tv: false
Type is like: iphone6plus
// iphone 7 plus landscape
width: 736.0
height: 414.0
maxLength: 736.0
minLength: 414.0
zoomed: true
retina: true
phone: true
pad: false
carplay: false
tv: false
Type is like: iphone6plus
// iphone x
width: 375.0
height: 812.0
maxLength: 812.0
minLength: 375.0
zoomed: true
retina: true
phone: true
pad: false
carplay: false
tv: false
Type is like: iphoneX
@jemai

This comment has been minimized.

Copy link

@jemai jemai commented Dec 15, 2016

Cool tool, cheers 👍

@keisei1092

This comment has been minimized.

Copy link

@keisei1092 keisei1092 commented Jan 26, 2017

👍

@hlazarpesic

This comment has been minimized.

Copy link

@hlazarpesic hlazarpesic commented Sep 19, 2017

This is awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment