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
}
static public func setup(screenType: ScreenType, scale: CGFloat = 2.0, isPortrait: Bool = true) -> (UIWindow, DeviceViewController) {
let vc = DeviceViewController()
vc.view.backgroundColor = .white
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 {
var size : CGSize
switch screenType {
case .iPhone3_5Inch:
size = CGSize(width: 320, height: 480)
case .iPhone4Inch:
size = CGSize(width: 320, height: 568)
case .iPhone4_7Inch:
size = CGSize(width: 375, height: 667)
case .iPhone5_5Inch:
size = CGSize(width: 414, height: 736)
case .iPad:
size = CGSize(width: 768, height: 1024)
case .iPad_12_9Inch:
size = CGSize(width: 1024, height: 1366)
case .tv:
size = CGSize(width: 1980, height: 1020)
}
if isPortrait {
return size
}
return CGSize.init(width: size.height, height: size.width)
}
}
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