Skip to content

Commit c172bee

Browse files
committed
Refactor header view controller
1 parent 7f51fe5 commit c172bee

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

Sources/Controllers/HeaderViewController.swift

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import UIKit
22

3+
/// Delegate to handle touch event of the close button.
34
public protocol HeaderViewControllerDelegate: class {
45
func headerViewControllerDidTapCloseButton(_ controller: HeaderViewController)
56
}
67

8+
/// Controller with title label and close button.
9+
/// It will be added as a child view controller if `BarcodeScannerController` is being presented.
710
public final class HeaderViewController: UIViewController {
811
public weak var delegate: HeaderViewControllerDelegate?
912

10-
/// Header view with title and close button.
13+
/// Header view with title label and close button.
1114
public private(set) lazy var navigationBar: UINavigationBar = self.makeNavigationBar()
12-
/// Title label to show as a title view in navigation bar
15+
/// Title view of the navigation bar
1316
public private(set) lazy var titleLabel: UILabel = self.makeTitleLabel()
14-
/// Close button to show as a left bar button item in navigation bar.
17+
/// Left bar button item of the navigation bar.
1518
public private(set) lazy var closeButton: UIButton = self.makeCloseButton()
1619

1720
// MARK: - View lifecycle
@@ -23,26 +26,32 @@ public final class HeaderViewController: UIViewController {
2326
closeButton.addTarget(self, action: #selector(handleCloseButtonTap), for: .touchUpInside)
2427

2528
view.addSubview(navigationBar)
26-
navigationBar.translatesAutoresizingMaskIntoConstraints = false
27-
navigationBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
28-
navigationBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
29-
30-
if #available(iOS 11, *) {
31-
navigationBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
32-
} else {
33-
navigationBar.topAnchor.constraint(
34-
equalTo: topLayoutGuide.bottomAnchor).isActive = true
35-
}
29+
setupConstraints()
3630
}
3731

3832
// MARK: - Actions
3933

4034
@objc private func handleCloseButtonTap() {
4135
delegate?.headerViewControllerDidTapCloseButton(self)
4236
}
37+
38+
// MARK: - Layout
39+
40+
private func setupConstraints() {
41+
navigationBar.translatesAutoresizingMaskIntoConstraints = false
42+
navigationBar.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
43+
navigationBar.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
44+
45+
if #available(iOS 11, *) {
46+
navigationBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
47+
} else {
48+
navigationBar.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
49+
}
50+
}
4351
}
4452

45-
// MARK: - Subviews
53+
// MARK: - Subviews
54+
4655
private extension HeaderViewController {
4756
func makeNavigationBar() -> UINavigationBar {
4857
let navigationBar = UINavigationBar()

0 commit comments

Comments
 (0)