@@ -29,8 +29,8 @@ public protocol BarcodeScannerDismissalDelegate: class {
2929 */
3030open 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 {
0 commit comments