Skip to content

Commit bd0deb9

Browse files
authored
Merge pull request #135 from cashapp/entin/default-controls-view
Modernize DefaultControlsViewController
2 parents d1c84cd + f58bdb0 commit bd0deb9

File tree

1 file changed

+62
-41
lines changed

1 file changed

+62
-41
lines changed

Example/AccessibilitySnapshot/DefaultControlsViewController.swift

+62-41
Original file line numberDiff line numberDiff line change
@@ -19,65 +19,86 @@ import UIKit
1919

2020
final class DefaultControlsViewController: AccessibilityViewController {
2121

22-
// MARK: - Private Properties
22+
// MARK: - UIViewController
2323

24-
private let datePicker: UIDatePicker = .init()
24+
override func loadView() {
25+
view = View()
26+
}
2527

26-
private let pageControl: UIPageControl = .init()
28+
}
2729

28-
private let segmentedControl: UISegmentedControl = .init()
30+
// MARK: -
2931

30-
private let slider: UISlider = .init()
32+
private extension DefaultControlsViewController {
3133

32-
private let stepper: UIStepper = .init()
34+
final class View: UIView {
3335

34-
private var controls: [UIControl] {
35-
return [
36-
datePicker,
37-
pageControl,
38-
segmentedControl,
39-
slider,
40-
stepper,
41-
]
42-
}
36+
// MARK: - Life Cycle
4337

44-
// MARK: - UIViewController
38+
override init(frame: CGRect) {
39+
super.init(frame: frame)
4540

46-
override func viewDidLoad() {
47-
super.viewDidLoad()
41+
pageControl.numberOfPages = 3
42+
pageControl.pageIndicatorTintColor = .lightGray
43+
pageControl.currentPageIndicatorTintColor = .black
4844

49-
pageControl.numberOfPages = 3
50-
pageControl.pageIndicatorTintColor = .lightGray
51-
pageControl.currentPageIndicatorTintColor = .black
45+
segmentedControl.insertSegment(withTitle: "Segment A", at: 0, animated: false)
46+
segmentedControl.insertSegment(withTitle: "Segment B", at: 1, animated: false)
47+
segmentedControl.insertSegment(withTitle: "Segment C", at: 2, animated: false)
5248

53-
segmentedControl.insertSegment(withTitle: "Segment A", at: 0, animated: false)
54-
segmentedControl.insertSegment(withTitle: "Segment B", at: 1, animated: false)
55-
segmentedControl.insertSegment(withTitle: "Segment C", at: 2, animated: false)
49+
slider.minimumValue = 0
50+
slider.maximumValue = 100
51+
slider.value = 75
5652

57-
slider.minimumValue = 0
58-
slider.maximumValue = 100
59-
slider.value = 75
53+
stepper.minimumValue = -1
54+
stepper.maximumValue = 1
55+
stepper.value = 0
6056

61-
stepper.minimumValue = -1
62-
stepper.maximumValue = 1
63-
stepper.value = 0
57+
controls.forEach { addSubview($0) }
58+
}
6459

65-
controls.forEach { view.addSubview($0) }
66-
}
60+
@available(*, unavailable)
61+
required init?(coder: NSCoder) {
62+
fatalError("init(coder:) has not been implemented")
63+
}
64+
65+
// MARK: - Private Properties
6766

68-
override func viewDidLayoutSubviews() {
69-
super.viewDidLayoutSubviews()
67+
private let datePicker: UIDatePicker = .init()
7068

71-
controls.forEach { $0.sizeToFit() }
69+
private let pageControl: UIPageControl = .init()
7270

73-
let statusBarHeight = UIApplication.shared.statusBarFrame.height
71+
private let segmentedControl: UISegmentedControl = .init()
7472

75-
var distributionSpecifiers: [ViewDistributionSpecifying] = [ statusBarHeight.fixed, 1.flexible ]
76-
for subview in controls {
77-
distributionSpecifiers.append(subview)
78-
distributionSpecifiers.append(1.flexible)
73+
private let slider: UISlider = .init()
74+
75+
private let stepper: UIStepper = .init()
76+
77+
private var controls: [UIControl] {
78+
return [
79+
datePicker,
80+
pageControl,
81+
segmentedControl,
82+
slider,
83+
stepper,
84+
]
85+
}
86+
87+
// MARK: - UIView
88+
89+
override func layoutSubviews() {
90+
controls.forEach { $0.sizeToFit() }
91+
92+
let statusBarHeight = window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
93+
94+
var distributionSpecifiers: [ViewDistributionSpecifying] = [ statusBarHeight.fixed, 1.flexible ]
95+
for subview in controls {
96+
distributionSpecifiers.append(subview)
97+
distributionSpecifiers.append(1.flexible)
98+
}
99+
applySubviewDistribution(distributionSpecifiers)
79100
}
80-
view.applySubviewDistribution(distributionSpecifiers)
101+
81102
}
82103

83104
}

0 commit comments

Comments
 (0)