Skip to content

Commit 19f6c2c

Browse files
authored
Merge pull request #77 from prcodes/develop
Fixing iPhone X issues, Simulator crash
2 parents afc7174 + ecfe4a0 commit 19f6c2c

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

Sources/BarcodeScannerController.swift

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public protocol BarcodeScannerDismissalDelegate: class {
2929
*/
3030
open class BarcodeScannerController: UIViewController {
3131

32-
/// Video capture device.
33-
lazy var captureDevice: AVCaptureDevice = AVCaptureDevice.default(for: AVMediaType.video)!
32+
/// Video capture device. This may be nil when running in Simulator.
33+
lazy var captureDevice: AVCaptureDevice! = AVCaptureDevice.default(for: AVMediaType.video)
3434

3535
/// Capture session.
3636
lazy var captureSession: AVCaptureSession = AVCaptureSession()
@@ -124,13 +124,13 @@ open class BarcodeScannerController: UIViewController {
124124
})
125125
}
126126
}
127-
127+
128128
public var barCodeFocusViewType: FocusViewType = .animated
129129

130130
/// The current torch mode on the capture device.
131131
var torchMode: TorchMode = .off {
132132
didSet {
133-
guard captureDevice.hasFlash else { return }
133+
guard let captureDevice = captureDevice, captureDevice.hasFlash else { return }
134134

135135
do {
136136
try captureDevice.lockForConfiguration()
@@ -264,6 +264,10 @@ open class BarcodeScannerController: UIViewController {
264264
Sets up capture input, output and session.
265265
*/
266266
func setupSession() {
267+
guard let captureDevice = captureDevice else {
268+
return
269+
}
270+
267271
do {
268272
let input = try AVCaptureDeviceInput(device: captureDevice)
269273
captureSession.addInput(input)
@@ -320,8 +324,27 @@ open class BarcodeScannerController: UIViewController {
320324

321325
// MARK: - Layout
322326
func setupFrame() {
323-
headerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 64)
324-
flashButton.frame = CGRect(x: view.frame.width - 50, y: 73, width: 37, height: 37)
327+
let flashButtonSize: CGFloat = 37
328+
let isLandscape = view.frame.width > view.frame.height
329+
330+
var rightSafeAreaInset: CGFloat = 0
331+
var topSafeAreaInset: CGFloat = 0
332+
if #available(iOS 11.0, *) {
333+
rightSafeAreaInset = view.safeAreaInsets.right
334+
topSafeAreaInset = view.safeAreaInsets.top
335+
}
336+
337+
var navbarSize: CGFloat = 0
338+
if (isLandscape) {
339+
navbarSize = 32
340+
}
341+
else {
342+
// On iPhone X devices, extend the size of the top nav bar
343+
navbarSize = topSafeAreaInset > 0 ? 88 : 64
344+
}
345+
346+
headerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: navbarSize)
347+
flashButton.frame = CGRect(x: view.frame.width - 50 - rightSafeAreaInset, y: navbarSize + 10 + (flashButtonSize / 2), width: flashButtonSize, height: flashButtonSize)
325348
infoView.frame = infoFrame
326349

327350
if let videoPreviewLayer = videoPreviewLayer {

Sources/HeaderView.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,24 @@ class HeaderView: UIView {
6161
override func layoutSubviews() {
6262
super.layoutSubviews()
6363

64-
let padding: CGFloat = 8
64+
var leftSafeAreaPadding: CGFloat = 0
65+
var topSafeAreaPadding: CGFloat = 0
66+
if #available(iOS 11, *) {
67+
leftSafeAreaPadding = safeAreaInsets.left
68+
topSafeAreaPadding = safeAreaInsets.top
69+
}
70+
71+
let leadingPadding: CGFloat = 15 + leftSafeAreaPadding
72+
let topPadding: CGFloat = 8 + topSafeAreaPadding
6573
let labelHeight: CGFloat = 40
66-
74+
6775
button.sizeToFit()
6876

69-
button.frame.origin = CGPoint(x: 15,
70-
y: ((frame.height - button.frame.height) / 2) + padding)
77+
button.frame.origin = CGPoint(x: leadingPadding,
78+
y: ((frame.height - button.frame.height) / 2) + topPadding)
7179

7280
label.frame = CGRect(
73-
x: 0, y: ((frame.height - labelHeight) / 2) + padding,
81+
x: 0, y: ((frame.height - labelHeight) / 2) + topPadding,
7482
width: frame.width, height: labelHeight)
7583
}
7684

Sources/InfoView.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ class InfoView: UIVisualEffectView {
8585
*/
8686
override func layoutSubviews() {
8787
super.layoutSubviews()
88+
89+
var leftSafeAreaPadding: CGFloat = 0
90+
var rightSafeAreaPadding: CGFloat = 0
91+
92+
if #available(iOS 11.0, *) {
93+
leftSafeAreaPadding = safeAreaInsets.left
94+
rightSafeAreaPadding = safeAreaInsets.right
95+
}
8896

8997
let padding: CGFloat = 10
9098
let labelHeight: CGFloat = 40
@@ -93,15 +101,15 @@ class InfoView: UIVisualEffectView {
93101

94102
if status.state != .processing && status.state != .notFound {
95103
imageView.frame = CGRect(
96-
x: padding,
104+
x: padding + leftSafeAreaPadding,
97105
y: (frame.height - imageSize.height) / 2,
98106
width: imageSize.width,
99107
height: imageSize.height)
100108

101109
label.frame = CGRect(
102110
x: imageView.frame.maxX + padding,
103111
y: 0,
104-
width: frame.width - imageView.frame.maxX - 2 * padding,
112+
width: frame.width - imageView.frame.maxX - 2 * padding - rightSafeAreaPadding,
105113
height: frame.height)
106114
} else {
107115
imageView.frame = CGRect(

0 commit comments

Comments
 (0)