Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
142 lines (128 sloc) 5.14 KB
import UIKit
public enum Device {
case phone3_5inch
case phone4inch
case phone4_7inch
case phone5_5inch
case pad
case pad12_9inch
}
public enum Orientation {
case portrait
case landscape
}
/**
Creates a controller that represents a specific device, orientation with specific traits.
- parameter device: The device the controller should represent.
- parameter orientation: The orientation of the device.
- parameter child: An optional controller to put inside the parent controller. If omitted
a blank controller will be used.
- parameter additionalTraits: An optional set of traits that will also be applied. Traits in this collection
will trump any traits derived from the device/orientation comboe specified.
- returns: Two controllers: a root controller that can be set to the playground's live view, and a content
controller which should have UI elements added to it
*/
public func playgroundControllers(device: Device = .phone4_7inch,
orientation: Orientation = .portrait,
child: UIViewController = UIViewController(),
additionalTraits: UITraitCollection = .init())
-> (parent: UIViewController, child: UIViewController) {
let parent = UIViewController()
parent.view.backgroundColor = .white
child.view.backgroundColor = .white
child.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
parent.addChild(child)
parent.view.addSubview(child.view)
child.didMove(toParent: parent)
child.view.frame = parent.view.frame
let traits: UITraitCollection
switch (device, orientation) {
case (.phone3_5inch, .portrait):
parent.view.frame = .init(x: 0, y: 0, width: 320, height: 480)
traits = .init(traitsFrom: [
.init(horizontalSizeClass: .compact),
.init(verticalSizeClass: .regular),
.init(userInterfaceIdiom: .phone)
])
case (.phone3_5inch, .landscape):
parent.view.frame = .init(x: 0, y: 0, width: 480, height: 320)
traits = .init(traitsFrom: [
.init(horizontalSizeClass: .compact),
.init(verticalSizeClass: .compact),
.init(userInterfaceIdiom: .phone)
])
case (.phone4inch, .portrait):
parent.view.frame = .init(x: 0, y: 0, width: 320, height: 568)
traits = .init(traitsFrom: [
.init(horizontalSizeClass: .compact),
.init(verticalSizeClass: .regular),
.init(userInterfaceIdiom: .phone)
])
case (.phone4inch, .landscape):
parent.view.frame = .init(x: 0, y: 0, width: 568, height: 320)
traits = .init(traitsFrom: [
.init(horizontalSizeClass: .compact),
.init(verticalSizeClass: .compact),
.init(userInterfaceIdiom: .phone)
])
case (.phone4_7inch, .portrait):
parent.view.frame = .init(x: 0, y: 0, width: 375, height: 667)
traits = .init(traitsFrom: [
.init(horizontalSizeClass: .compact),
.init(verticalSizeClass: .regular),
.init(userInterfaceIdiom: .phone)
])
case (.phone4_7inch, .landscape):
parent.view.frame = .init(x: 0, y: 0, width: 667, height: 375)
traits = .init(traitsFrom: [
.init(horizontalSizeClass: .compact),
.init(verticalSizeClass: .compact),
.init(userInterfaceIdiom: .phone)
])
case (.phone5_5inch, .portrait):
parent.view.frame = .init(x: 0, y: 0, width: 414, height: 736)
traits = .init(traitsFrom: [
.init(horizontalSizeClass: .compact),
.init(verticalSizeClass: .regular),
.init(userInterfaceIdiom: .phone)
])
case (.phone5_5inch, .landscape):
parent.view.frame = .init(x: 0, y: 0, width: 736, height: 414)
traits = .init(traitsFrom: [
.init(horizontalSizeClass: .regular),
.init(verticalSizeClass: .compact),
.init(userInterfaceIdiom: .phone)
])
case (.pad, .portrait):
parent.view.frame = .init(x: 0, y: 0, width: 768, height: 1024)
traits = .init(traitsFrom: [
.init(horizontalSizeClass: .regular),
.init(verticalSizeClass: .regular),
.init(userInterfaceIdiom: .pad)
])
case (.pad, .landscape):
parent.view.frame = .init(x: 0, y: 0, width: 1024, height: 768)
traits = .init(traitsFrom: [
.init(horizontalSizeClass: .regular),
.init(verticalSizeClass: .regular),
.init(userInterfaceIdiom: .pad)
])
case (.pad12_9inch, .portrait):
parent.view.frame = .init(x: 0, y: 0, width: 1024, height: 1366)
traits = .init(traitsFrom: [
.init(horizontalSizeClass: .regular),
.init(verticalSizeClass: .regular),
.init(userInterfaceIdiom: .pad)
])
case (.pad12_9inch, .landscape):
parent.view.frame = .init(x: 0, y: 0, width: 1366, height: 1024)
traits = .init(traitsFrom: [
.init(horizontalSizeClass: .regular),
.init(verticalSizeClass: .regular),
.init(userInterfaceIdiom: .pad)
])
}
let allTraits = UITraitCollection.init(traitsFrom: [traits, additionalTraits])
parent.setOverrideTraitCollection(allTraits, forChild: child)
return (parent, child)
}
You can’t perform that action at this time.