-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathCustomSplitViewController.swift
81 lines (66 loc) · 2.34 KB
/
CustomSplitViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// CustomSplitViewController.swift
// MullvadVPN
//
// Created by pronebird on 07/04/2021.
// Copyright © 2021 Mullvad VPN AB. All rights reserved.
//
import UIKit
class CustomSplitViewController: UISplitViewController, RootContainment {
var preferredHeaderBarPresentation: HeaderBarPresentation {
for case let viewController as any RootContainment in viewControllers {
return viewController.preferredHeaderBarPresentation
}
return .default
}
var prefersHeaderBarHidden: Bool {
for case let viewController as any RootContainment in viewControllers {
return viewController.prefersHeaderBarHidden
}
return false
}
var dividerColor: UIColor? {
didSet {
if isViewLoaded {
self.updateDividerColor()
}
}
}
override var childForStatusBarStyle: UIViewController? {
super.childForStatusBarStyle
}
override var childForStatusBarHidden: UIViewController? {
super.childForStatusBarHidden
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
updateDividerColor()
}
private var dividerView: UIView? {
let subviews = view.subviews.flatMap { view -> [UIView] in
[view] + view.subviews
}
return subviews.first { view -> Bool in
view.description.hasPrefix("<UIPanelBorderView")
}
}
private func updateDividerColor() {
guard let dividerColor else { return }
dividerView?.backgroundColor = dividerColor
}
override func overrideTraitCollection(forChild childViewController: UIViewController)
-> UITraitCollection? {
guard let traitCollection = super.overrideTraitCollection(forChild: childViewController)
else { return nil }
// Pass the split controller's horizontal size class to the primary controller when split
// view is expanded.
if !isCollapsed, childViewController == viewControllers.last {
let sizeOverrideTraitCollection = UITraitCollection(
horizontalSizeClass: self.traitCollection.horizontalSizeClass
)
return UITraitCollection(traitsFrom: [traitCollection, sizeOverrideTraitCollection])
} else {
return traitCollection
}
}
}