From 06f38e0b538095e1aa3f648a8feec9e4da2d7311 Mon Sep 17 00:00:00 2001 From: Julian Finkler Date: Mon, 16 Dec 2019 20:59:07 +0100 Subject: [PATCH] Fix for Issue #61 While the iOS device is rotating the bounds of the previewView will be recalculated and the scanRect will be reinitialized Changes in the WorkspaceSettings are necessary to run the example app --- .../xcshareddata/WorkspaceSettings.xcsettings | 10 +++++ ios/Classes/BarcodeScannerViewController.m | 44 ++++++++++++------- 2 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings diff --git a/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings new file mode 100644 index 0000000..6b30c74 --- /dev/null +++ b/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings @@ -0,0 +1,10 @@ + + + + + BuildSystemType + Original + PreviewsEnabled + + + diff --git a/ios/Classes/BarcodeScannerViewController.m b/ios/Classes/BarcodeScannerViewController.m index da44ca0..0366f0d 100644 --- a/ios/Classes/BarcodeScannerViewController.m +++ b/ios/Classes/BarcodeScannerViewController.m @@ -10,6 +10,34 @@ @implementation BarcodeScannerViewController { } +- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator +{ + CGRect bounds = [UIScreen mainScreen].bounds; + CGRect reversedBounds = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.height, bounds.size.width); + self.previewView.bounds = reversedBounds; + self.previewView.frame = reversedBounds; + [self.scanRect removeFromSuperview]; + [self setupScanRect:reversedBounds]; + [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; +} + +- (void)setupScanRect:(CGRect)bounds { + self.scanRect = [[ScannerOverlay alloc] initWithFrame:bounds]; + self.scanRect.translatesAutoresizingMaskIntoConstraints = NO; + self.scanRect.backgroundColor = UIColor.clearColor; + [self.view addSubview:_scanRect]; + [self.view addConstraints:[NSLayoutConstraint + constraintsWithVisualFormat:@"V:[scanRect]" + options:NSLayoutFormatAlignAllBottom + metrics:nil + views:@{@"scanRect": _scanRect}]]; + [self.view addConstraints:[NSLayoutConstraint + constraintsWithVisualFormat:@"H:[scanRect]" + options:NSLayoutFormatAlignAllBottom + metrics:nil + views:@{@"scanRect": _scanRect}]]; + [_scanRect startAnimating]; +} - (void)viewDidLoad { [super viewDidLoad]; @@ -26,21 +54,7 @@ options:NSLayoutFormatAlignAllBottom metrics:nil views:@{@"previewView": _previewView}]]; - self.scanRect = [[ScannerOverlay alloc] initWithFrame:self.view.bounds]; - self.scanRect.translatesAutoresizingMaskIntoConstraints = NO; - self.scanRect.backgroundColor = UIColor.clearColor; - [self.view addSubview:_scanRect]; - [self.view addConstraints:[NSLayoutConstraint - constraintsWithVisualFormat:@"V:[scanRect]" - options:NSLayoutFormatAlignAllBottom - metrics:nil - views:@{@"scanRect": _scanRect}]]; - [self.view addConstraints:[NSLayoutConstraint - constraintsWithVisualFormat:@"H:[scanRect]" - options:NSLayoutFormatAlignAllBottom - metrics:nil - views:@{@"scanRect": _scanRect}]]; - [_scanRect startAnimating]; + [self setupScanRect:self.view.bounds]; self.scanner = [[MTBBarcodeScanner alloc] initWithPreviewView:_previewView]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)]; [self updateFlashButton];