This repository has been archived by the owner on Feb 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 267
/
OverlayViewController.swift
89 lines (76 loc) · 3.27 KB
/
OverlayViewController.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
82
83
84
85
86
87
88
89
// Copyright 2017 LinkedIn Corp.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import UIKit
import LayoutKit
import ExampleLayouts
class OverlayViewController: UIViewController {
// MARK: - constants
// A smattering of alignments to test for the overlay and underlay views
private static let alignments: [Alignment] = [
.topLeading,
.topTrailing,
.topCenter,
.bottomLeading,
.bottomTrailing,
.bottomCenter,
.centerLeading,
.centerTrailing,
.center,
.fill
]
// MARK: - variables
private var tableView = LayoutAdapterTableView(frame: .zero, style: .grouped)
private var cachedLayouts: [Layout] = {
return (0..<alignments.count).flatMap { backgroundCount in
return (0..<alignments.count).map { overlayCount in
let backgroundLayouts = (0...backgroundCount).map { index in
return SizeLayout(
width: 60,
height: 40,
alignment: alignments[index],
config: { $0.backgroundColor = .orange })
}
let overlayLayouts = (0...overlayCount).map { index in
return SizeLayout(
width: 40,
height: 80,
alignment: alignments[index],
config: { $0.backgroundColor = .yellow })
}
let text = "Primary alignment is me!\n"
+ "Background: \(backgroundCount + 1) views (orange)\n"
+ "Overlay: \(overlayCount + 1) views (yellow)"
let baseLayout = InsetLayout(
inset: 70,
sublayout: LabelLayout(text: text))
return OverlayLayout(
primaryLayouts: [baseLayout],
backgroundLayouts: backgroundLayouts,
overlayLayouts: overlayLayouts)
}
}
}()
// MARK: - layout methods
override func viewDidLoad() {
super.viewDidLoad()
tableView.frame = view.bounds
tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
tableView.backgroundColor = UIColor.purple
view.addSubview(tableView)
self.layoutOverlays(width: tableView.frame.width, synchronous: false)
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
layoutOverlays(width: size.width, synchronous: true)
}
private func layoutOverlays(width: CGFloat, synchronous: Bool) {
tableView.layoutAdapter.reload(width: width, synchronous: synchronous, layoutProvider: { [weak self] in
return [Section(header: nil, items: self?.cachedLayouts ?? [], footer: nil)]
})
}
}