Skip to content

Instantly share code, notes, and snippets.

@nbasham
Last active May 8, 2017 11:08
Show Gist options
  • Save nbasham/c6af488949dc3435e43b3d3cba9d4df4 to your computer and use it in GitHub Desktop.
Save nbasham/c6af488949dc3435e43b3d3cba9d4df4 to your computer and use it in GitHub Desktop.
A sizable UIViewController that can be used as a playground liveView and set to specific device sizes.
import UIKit
import PlaygroundSupport
public class DeviceViewController : UIViewController {
public enum ScreenType : Int {
case iPhone3_5Inch
case iPhone4Inch // includes 5th & 6h gen iPod Touch
case iPhone4_7Inch
case iPhone5_5Inch
case iPad
case iPad_12_9Inch
case tv
}
override public func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
static public func setup(screenType: ScreenType, scale: CGFloat = 2.0, isPortrait: Bool = true) -> (UIWindow, DeviceViewController) {
let vc = DeviceViewController()
vc.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
let screenSize = DeviceViewController.screenTypeSize(screenType, isPortrait: isPortrait)
let playgroundSize = CGSize.init(width: screenSize.width*scale, height: screenSize.height*scale)
let w = UIWindow(frame: CGRect(x: 0, y: 0, width: playgroundSize.width, height: playgroundSize.height))
w.rootViewController = vc
w.makeKeyAndVisible()
return (w, vc)
}
static private func screenTypeSize(_ screenType:ScreenType, isPortrait:Bool = true) -> CGSize {
switch screenType {
case .iPhone3_5Inch:
return isPortrait ? CGSize(width: 320, height: 480) : CGSize(width: 480, height: 320)
case .iPhone4Inch:
return isPortrait ? CGSize(width: 320, height: 568) : CGSize(width: 568, height: 320)
case .iPhone4_7Inch:
return isPortrait ? CGSize(width: 375, height: 667) : CGSize(width: 667, height: 375)
case .iPhone5_5Inch:
return isPortrait ? CGSize(width: 414, height: 736) : CGSize(width: 736, height: 414)
case .iPad:
return isPortrait ? CGSize(width: 768, height: 1024) : CGSize(width: 1024, height: 768)
case .iPad_12_9Inch:
return isPortrait ? CGSize(width: 1024, height: 1366) : CGSize(width: 1366, height: 1024)
case .tv:
return CGSize(width: 1980, height: 1020)
}
}
}
let (window, vc) = DeviceViewController.setup(screenType: .iPhone3_5Inch/*, scale: 2.0, isPortrait: true*/)
PlaygroundPage.current.liveView = window
vc.view.backgroundColor = .orange
@SmatchyLaPaglia
Copy link

Nice! Any plans to update for modern screen sizes?

@nbasham
Copy link
Author

nbasham commented Feb 17, 2017

A more flexible approach found here.

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