|
1 | 1 | import UIKit |
2 | 2 |
|
3 | | -protocol HeaderViewDelegate: class { |
4 | | - func headerViewDidPressClose(_ headerView: HeaderView) |
5 | | -} |
6 | | - |
7 | | -/** |
8 | | - Header view that simulates a navigation bar. |
9 | | - */ |
10 | | -final class HeaderView: UIView { |
11 | | - /// Title label. |
12 | | - private lazy var label: UILabel = { |
| 3 | +/// UI elements on the navigation bar |
| 4 | +final class HeaderElement { |
| 5 | + static func makeLabel() -> UILabel { |
13 | 6 | let label = UILabel() |
14 | 7 | label.text = Title.text |
15 | 8 | label.font = Title.font |
16 | 9 | label.textColor = Title.color |
17 | | - label.backgroundColor = Title.backgroundColor |
18 | 10 | label.numberOfLines = 1 |
19 | 11 | label.textAlignment = .center |
20 | 12 | return label |
21 | | - }() |
| 13 | + } |
22 | 14 |
|
23 | | - /// Close button. |
24 | | - private lazy var button: UIButton = { |
| 15 | + static func makeCloseButton() -> UIButton { |
25 | 16 | let button = UIButton(type: .system) |
26 | 17 | button.setTitle(CloseButton.text, for: UIControlState()) |
27 | 18 | button.titleLabel?.font = CloseButton.font |
28 | 19 | button.tintColor = CloseButton.color |
29 | | - button.addTarget(self, action: #selector(buttonDidPress), for: .touchUpInside) |
30 | 20 | return button |
31 | | - }() |
32 | | - |
33 | | - /// Header view delegate. |
34 | | - weak var delegate: HeaderViewDelegate? |
35 | | - |
36 | | - // MARK: - Initialization |
37 | | - |
38 | | - /** |
39 | | - Creates a new instance of `HeaderView`. |
40 | | - |
41 | | - - Parameter frame: View frame. |
42 | | - */ |
43 | | - override init(frame: CGRect) { |
44 | | - super.init(frame: frame) |
45 | | - |
46 | | - backgroundColor = Title.backgroundColor |
47 | | - |
48 | | - [label, button].forEach { |
49 | | - addSubview($0) |
50 | | - } |
51 | | - } |
52 | | - |
53 | | - required init?(coder aDecoder: NSCoder) { |
54 | | - fatalError("init(coder:) has not been implemented") |
55 | | - } |
56 | | - |
57 | | - // MARK: - Layout |
58 | | - |
59 | | - override func layoutSubviews() { |
60 | | - super.layoutSubviews() |
61 | | - |
62 | | - let insets = viewInsets |
63 | | - let leadingPadding: CGFloat = 15 + insets.left |
64 | | - let topPadding: CGFloat = 8 + insets.top |
65 | | - let labelHeight: CGFloat = 40 |
66 | | - |
67 | | - button.sizeToFit() |
68 | | - |
69 | | - button.frame.origin = CGPoint( |
70 | | - x: leadingPadding, |
71 | | - y: ((frame.height - button.frame.height) / 2) + topPadding |
72 | | - ) |
73 | | - |
74 | | - label.frame = CGRect( |
75 | | - x: 0, y: ((frame.height - labelHeight) / 2) + topPadding, |
76 | | - width: frame.width, height: labelHeight |
77 | | - ) |
78 | | - } |
79 | | - |
80 | | - // MARK: - Actions |
81 | | - |
82 | | - /** |
83 | | - Close button action handler. |
84 | | - */ |
85 | | - @objc private func buttonDidPress() { |
86 | | - delegate?.headerViewDidPressClose(self) |
87 | 21 | } |
88 | 22 | } |
0 commit comments