HeaderViewController 2.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import UIKit
  2. /// Delegate to handle touch event of the close button.
  3. protocol HeaderViewControllerDelegate: class {
  4. func headerViewControllerDidTapCloseButton(_ controller: HeaderViewController)
  5. }
  6. /// View controller with title label and close button.
  7. /// It will be added as a child view controller if `BarcodeScannerController` is being presented.
  8. public final class HeaderViewController: UIViewController {
  9. weak var delegate: HeaderViewControllerDelegate?
  10. // MARK: - UI properties
  11. /// Header view with title label and close button.
  12. public private(set) lazy var navigationBar: UINavigationBar = self.makeNavigationBar()
  13. /// Title view of the navigation bar.
  14. public private(set) lazy var titleLabel: UILabel = self.makeTitleLabel()
  15. /// Left bar button item of the navigation bar.
  16. public private(set) lazy var closeButton: UIButton = self.makeCloseButton()
  17. // MARK: - View lifecycle
  18. public override func viewDidLoad() {
  19. super.viewDidLoad()
  20. navigationBar.delegate = self
  21. closeButton.addTarget(self, action: #selector(handleCloseButtonTap), for: .touchUpInside)
  22. view.addSubview(navigationBar)
  23. setupConstraints()
  24. }
  25. // MARK: - Actions
  26. @objc private func handleCloseButtonTap() {
  27. delegate?.headerViewControllerDidTapCloseButton(self)
  28. }
  29. // MARK: - Layout
  30. private func setupConstraints() {
  31. NSLayoutConstraint.activate(
  32. navigationBar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  33. navigationBar.trailingAnchor.constraint(equalTo: view.trailingAnchor)
  34. )
  35. if #available(iOS 11, *) {
  36. navigationBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
  37. } else {
  38. navigationBar.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
  39. }
  40. }
  41. }
  42. // MARK: - Subviews factory
  43. private extension HeaderViewController {
  44. func makeNavigationBar() -> UINavigationBar {
  45. let navigationBar = UINavigationBar()
  46. navigationBar.isTranslucent = false
  47. navigationBar.backgroundColor = .white
  48. navigationBar.items = [makeNavigationItem()]
  49. return navigationBar
  50. }
  51. func makeNavigationItem() -> UINavigationItem {
  52. let navigationItem = UINavigationItem()
  53. closeButton.sizeToFit()
  54. navigationItem.leftBarButtonItem = UIBarButtonItem(customView: closeButton)
  55. titleLabel.sizeToFit()
  56. navigationItem.titleView = titleLabel
  57. return navigationItem
  58. }
  59. func makeTitleLabel() -> UILabel {
  60. let label = UILabel()
  61. label.text = localizedString("SCAN_BARCODE_TITLE")
  62. label.font = UIFont.boldSystemFont(ofSize: 17)
  63. label.textColor = .black
  64. label.numberOfLines = 1
  65. label.textAlignment = .center
  66. return label
  67. }
  68. func makeCloseButton() -> UIButton {
  69. let button = UIButton(type: .system)
  70. button.setTitle(localizedString("BUTTON_CLOSE"), for: UIControlState())
  71. button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
  72. button.tintColor = .black
  73. return button
  74. }
  75. }
  76. // MARK: - UINavigationBarDelegate
  77. extension HeaderViewController: UINavigationBarDelegate {
  78. public func position(for bar: UIBarPositioning) -> UIBarPosition {
  79. return .topAttached
  80. }
  81. }