This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Suggests display mode based on trait collection | |
private func suggestDisplayMode(for traitCollection: UITraitCollection) -> DisplayMode { | |
return traitCollection.horizontalSizeClass == .regular ? .sideBySide : .one | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class ContainerView: UIView { | |
var contentView: UIView? { | |
willSet { | |
contentView?.removeFromSuperview() | |
} | |
didSet { | |
guard let contentView = contentView else { return } | |
addSubview(contentView) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func viewWillLayoutSubviews() { | |
super.viewWillLayoutSubviews() | |
// Update display state | |
displayMode = suggestDisplayMode(for: traitCollection) | |
let size = view.bounds.size | |
switch displayMode { | |
case .sideBySide: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
OlderNewer