Skip to content

Instantly share code, notes, and snippets.

View dmytro-anokhin's full-sized avatar
🔨

Dmytro Anokhin dmytro-anokhin

🔨
View GitHub Profile
@dmytro-anokhin
dmytro-anokhin / DisplayMode.swift
Last active October 8, 2017 10:46
DisplayMode
enum DisplayMode {
// Both view controllers are displayed at the same time, side-by-side
case sideBySide
// One view controller at a time is displayed
case one
}
private(set) var displayMode: DisplayMode = .sideBySide
/// Suggests display mode based on trait collection
private func suggestDisplayMode(for traitCollection: UITraitCollection) -> DisplayMode {
return traitCollection.horizontalSizeClass == .regular ? .sideBySide : .one
}
/// Defines visible view controller when one at a time displayed
private enum DisplayFocus {
/// When .displayMode set to .one, left view is visible
case left
/// When .displayMode set to .one, right view is visible
case right
}
private var displayFocus: DisplayFocus = .left
/// Width of the left view in side-by-side display mode
private var leftColumnWidth: CGFloat!
/// Suggests width of the left column in side-by-side display mode
private func suggestLeftColumnWidth(for availableSize: CGSize) -> CGFloat {
return availableSize.width > availableSize.height ? 440.0 : 320.0
}
private class ContainerView: UIView {
var contentView: UIView? {
willSet {
contentView?.removeFromSuperview()
}
didSet {
guard let contentView = contentView else { return }
addSubview(contentView)
private func fullFrame(for availableSize: CGSize) -> CGRect
private func fullLeftFrame(for availableSize: CGSize) -> CGRect
private func fullRightFrame(for availableSize: CGSize) -> CGRect
private func leftColumnFrame(for availableSize: CGSize) -> CGRect
private func rightColumnFrame(for availableSize: CGSize) -> CGRect
private func leftColumnFrame(for availableSize: CGSize) -> CGRect {
switch traitCollection.layoutDirection {
case .unspecified, .leftToRight:
return CGRect(x: 0.0, y: 0.0, width: leftColumnWidth, height: size.height)
case .rightToLeft:
return CGRect(x: size.width — leftColumnWidth, y: 0.0, width: leftColumnWidth, height: size.height)
}
}
var leftViewController: UIViewController? {
willSet {
guard let child = leftViewController else { return }
child.willMove(toParentViewController: nil)
child.view.removeFromSuperview()
child.removeFromParentViewController()
}
didSet {
guard let child = leftViewController else { return }
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
// Update display state
displayMode = suggestDisplayMode(for: traitCollection)
let size = view.bounds.size
switch displayMode {
case .sideBySide:
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
super.willTransition(to: newCollection, with: coordinator)
coordinator.animate(alongsideTransition: nil) { _ in
switch displayMode {
case .sideBySide:
navigationItem.rightBarButtonItem = nil
break
case .one:
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Flip", style: .plain, target: self, action: #selector(flipAction))