Skip to content

Commit 41f36f3

Browse files
committed
Modernize ElementOrderViewController
1 parent ba79aab commit 41f36f3

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

Example/AccessibilitySnapshot/ElementOrderViewController.swift

+42-23
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ final class ElementOrderViewController: AccessibilityViewController {
3838
// MARK: - Life Cycle
3939

4040
init(configurations: [ViewConfiguration]) {
41-
views = configurations.enumerated().map { index, configuration in
41+
super.init(nibName: nil, bundle: nil)
42+
43+
rootView.views = configurations.enumerated().map { index, configuration in
4244
let view: UIView
4345
switch configuration {
4446
case .element:
@@ -64,8 +66,6 @@ final class ElementOrderViewController: AccessibilityViewController {
6466

6567
return (view, configuration)
6668
}
67-
68-
super.init(nibName: nil, bundle: nil)
6969
}
7070

7171
@available(*, unavailable)
@@ -75,34 +75,53 @@ final class ElementOrderViewController: AccessibilityViewController {
7575

7676
// MARK: - Private Properties
7777

78-
private let views: [(UIView, ViewConfiguration)]
78+
private var rootView: View {
79+
return view as! View
80+
}
7981

8082
// MARK: - UIViewController
8183

82-
override func viewDidLoad() {
83-
super.viewDidLoad()
84-
85-
views.forEach { view.addSubview($0.0) }
84+
override func loadView() {
85+
view = View()
8686
}
8787

88-
override func viewDidLayoutSubviews() {
89-
super.viewDidLayoutSubviews()
88+
}
9089

91-
views.forEach {
92-
let (view, configuration) = $0
93-
switch configuration {
94-
case let .element(offset):
95-
view.align(
96-
.center,
97-
withSuperviewPosition: .center,
98-
horizontalOffset: offset.horizontal,
99-
verticalOffset: offset.vertical
100-
)
101-
102-
case .container, .viewWithAccessibleSubviews:
103-
view.alignToSuperview(.center)
90+
// MARK: -
91+
92+
private extension ElementOrderViewController {
93+
94+
final class View: UIView {
95+
96+
// MARK: - Public Properties
97+
98+
var views: [(UIView, ViewConfiguration)] = [] {
99+
didSet {
100+
oldValue.forEach { $0.0.removeFromSuperview() }
101+
views.forEach { addSubview($0.0) }
104102
}
105103
}
104+
105+
// MARK: - UIView
106+
107+
override func layoutSubviews() {
108+
views.forEach {
109+
let (view, configuration) = $0
110+
switch configuration {
111+
case let .element(offset):
112+
view.align(
113+
.center,
114+
withSuperviewPosition: .center,
115+
horizontalOffset: offset.horizontal,
116+
verticalOffset: offset.vertical
117+
)
118+
119+
case .container, .viewWithAccessibleSubviews:
120+
view.alignToSuperview(.center)
121+
}
122+
}
123+
}
124+
106125
}
107126

108127
}

0 commit comments

Comments
 (0)