Skip to content

Instantly share code, notes, and snippets.

@douglashill
Created June 23, 2020 21:05
Show Gist options
  • Save douglashill/b620f22288f264a950f9ede7afbb76e3 to your computer and use it in GitHub Desktop.
Save douglashill/b620f22288f264a950f9ede7afbb76e3 to your computer and use it in GitHub Desktop.
A sample UIKit app that sets up a four column layout with new iOS 14 API on UISplitViewController.
import UIKit
class FourColumnsContainerViewController: UIViewController {
let outerSplitViewController = UISplitViewController(style: .tripleColumn)
let innerSplitViewController = UISplitViewController(style: .doubleColumn)
let primary = makeContentViewController("App")
let secondary = makeContentViewController("Files")
let mainContent = makeContentViewController("File Content")
let inspector = makeContentViewController("Inspector")
init() {
outerSplitViewController.primaryEdge = .leading
innerSplitViewController.primaryEdge = .trailing
innerSplitViewController.preferredSplitBehavior = .tile
outerSplitViewController.maximumPrimaryColumnWidth = 120
innerSplitViewController.maximumPrimaryColumnWidth = 200
outerSplitViewController.primaryBackgroundStyle = .sidebar
innerSplitViewController.setViewController(mainContent, for: .secondary)
innerSplitViewController.setViewController(inspector, for: .primary)
outerSplitViewController.setViewController(primary, for: .primary)
outerSplitViewController.setViewController(secondary, for: .supplementary)
outerSplitViewController.setViewController(innerSplitViewController, for: .secondary)
// MARK: - Boring stuff below here
super.init(nibName: nil, bundle: nil)
addChild(outerSplitViewController)
outerSplitViewController.didMove(toParent: self)
}
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(outerSplitViewController.view)
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
outerSplitViewController.view.frame = view.bounds
}
@available(*, unavailable)
override var splitViewController: UISplitViewController? {
nil
}
@available(*, unavailable) required init?(coder: NSCoder) { preconditionFailure() }
}
private let makeContentViewController: (String) -> UINavigationController = { title in
let vc = UIViewController()
vc.title = title
vc.view.backgroundColor = .systemBackground
return UINavigationController(rootViewController: vc)
}
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var _window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let windowScene = scene as! UIWindowScene
let window = UIWindow(windowScene: windowScene)
window.rootViewController = FourColumnsContainerViewController()
window.makeKeyAndVisible()
_window = window
}
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment