Skip to content

Commit

Permalink
Merge pull request #82 from hyperoslo/fix/bar
Browse files Browse the repository at this point in the history
Use UINavigationBar
  • Loading branch information
onmyway133 authored Jan 10, 2018
2 parents 7564207 + 57c68b1 commit 508aba9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 82 deletions.
53 changes: 42 additions & 11 deletions Sources/BarcodeScannerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ open class BarcodeScannerController: UIViewController {
private lazy var captureSession: AVCaptureSession = AVCaptureSession()

/// Header view with title and close button.
private lazy var headerView: HeaderView = HeaderView()
private lazy var navigationBar: UINavigationBar = UINavigationBar()

/// Information view with description label.
private lazy var infoView: InfoView = InfoView()
Expand Down Expand Up @@ -186,27 +186,57 @@ open class BarcodeScannerController: UIViewController {

view.layer.addSublayer(videoPreviewLayer)

[infoView, headerView, settingsButton, flashButton, focusView].forEach {
[infoView, navigationBar, settingsButton, flashButton, focusView].forEach {
view.addSubview($0)
view.bringSubview(toFront: $0)
}

torchMode = .off
focusView.isHidden = true
headerView.delegate = self

setupCamera()

NotificationCenter.default.addObserver(
self, selector: #selector(appWillEnterForeground),
name: NSNotification.Name.UIApplicationWillEnterForeground,
object: nil)

// navigationBar
let item = UINavigationItem()
let closeButton = HeaderElement.makeCloseButton()
closeButton.addTarget(
self,
action: #selector(closeButtonDidTouched),
for: .touchUpInside
)
item.leftBarButtonItem = UIBarButtonItem(customView: closeButton)
item.titleView = HeaderElement.makeLabel()

navigationBar.isTranslucent = false
navigationBar.delegate = self
navigationBar.backgroundColor = Title.backgroundColor
navigationBar.items = [item]

navigationBar.translatesAutoresizingMaskIntoConstraints = false
navigationBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
navigationBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true

if #available(iOS 11, *) {
navigationBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
} else {
navigationBar.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
}
}

open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

headerView.isHidden = !isBeingPresented

if navigationController != nil {
navigationItem.titleView = HeaderElement.makeLabel()
navigationBar.isHidden = true
} else {
navigationBar.isHidden = false
}
}

open override func viewDidAppear(_ animated: Bool) {
Expand All @@ -232,6 +262,10 @@ open class BarcodeScannerController: UIViewController {
animateFocusView()
}

@objc private func closeButtonDidTouched() {
dismissalDelegate?.barcodeScannerDidDismiss(self)
}

// MARK: - Configuration

/**
Expand Down Expand Up @@ -329,7 +363,6 @@ open class BarcodeScannerController: UIViewController {
// On iPhone X devices, extend the size of the top nav bar
let navbarSize: CGFloat = isLandscape ? 32 : insets.top > 0 ? 88 : 64

headerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: navbarSize)
flashButton.frame = CGRect(
x: view.frame.width - 50 - insets.right,
y: navbarSize + 10 + (flashButtonSize / 2),
Expand Down Expand Up @@ -477,10 +510,8 @@ extension BarcodeScannerController: AVCaptureMetadataOutputObjectsDelegate {
}
}

// MARK: - HeaderViewDelegate

extension BarcodeScannerController: HeaderViewDelegate {
func headerViewDidPressClose(_ headerView: HeaderView) {
dismissalDelegate?.barcodeScannerDidDismiss(self)
extension BarcodeScannerController: UINavigationBarDelegate {
public func position(for bar: UIBarPositioning) -> UIBarPosition {
return .topAttached
}
}
76 changes: 5 additions & 71 deletions Sources/HeaderView.swift
Original file line number Diff line number Diff line change
@@ -1,88 +1,22 @@
import UIKit

protocol HeaderViewDelegate: class {
func headerViewDidPressClose(_ headerView: HeaderView)
}

/**
Header view that simulates a navigation bar.
*/
final class HeaderView: UIView {
/// Title label.
private lazy var label: UILabel = {
/// UI elements on the navigation bar
final class HeaderElement {
static func makeLabel() -> UILabel {
let label = UILabel()
label.text = Title.text
label.font = Title.font
label.textColor = Title.color
label.backgroundColor = Title.backgroundColor
label.numberOfLines = 1
label.textAlignment = .center
return label
}()
}

/// Close button.
private lazy var button: UIButton = {
static func makeCloseButton() -> UIButton {
let button = UIButton(type: .system)
button.setTitle(CloseButton.text, for: UIControlState())
button.titleLabel?.font = CloseButton.font
button.tintColor = CloseButton.color
button.addTarget(self, action: #selector(buttonDidPress), for: .touchUpInside)
return button
}()

/// Header view delegate.
weak var delegate: HeaderViewDelegate?

// MARK: - Initialization

/**
Creates a new instance of `HeaderView`.

- Parameter frame: View frame.
*/
override init(frame: CGRect) {
super.init(frame: frame)

backgroundColor = Title.backgroundColor

[label, button].forEach {
addSubview($0)
}
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// MARK: - Layout

override func layoutSubviews() {
super.layoutSubviews()

let insets = viewInsets
let leadingPadding: CGFloat = 15 + insets.left
let topPadding: CGFloat = 8 + insets.top
let labelHeight: CGFloat = 40

button.sizeToFit()

button.frame.origin = CGPoint(
x: leadingPadding,
y: ((frame.height - button.frame.height) / 2) + topPadding
)

label.frame = CGRect(
x: 0, y: ((frame.height - labelHeight) / 2) + topPadding,
width: frame.width, height: labelHeight
)
}

// MARK: - Actions

/**
Close button action handler.
*/
@objc private func buttonDidPress() {
delegate?.headerViewDidPressClose(self)
}
}

0 comments on commit 508aba9

Please sign in to comment.