Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Use safe areas and reposition elements on rotation #585

Merged
merged 2 commits into from
Feb 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 53 additions & 45 deletions src/ios/CDVBarcodeScanner.mm
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ @interface CDVbcsViewController : UIViewController <CDVBarcodeScannerOrientation
@property (nonatomic, retain) NSString* alternateXib;
@property (nonatomic) BOOL shutterPressed;
@property (nonatomic, retain) IBOutlet UIView* overlayView;
@property (nonatomic, retain) UIToolbar * toolbar;
@property (nonatomic, retain) UIView * reticleView;
// unsafe_unretained is equivalent to assign - used to prevent retain cycles in the property below
@property (nonatomic, unsafe_unretained) id orientationDelegate;

Expand Down Expand Up @@ -901,19 +903,6 @@ - (void)dealloc {
//--------------------------------------------------------------------------
- (void)loadView {
self.view = [[UIView alloc] initWithFrame: self.processor.parentViewController.view.frame];

// setup capture preview layer
AVCaptureVideoPreviewLayer* previewLayer = self.processor.previewLayer;
previewLayer.frame = self.view.bounds;
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

if ([previewLayer.connection isVideoOrientationSupported]) {
[previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
}

[self.view.layer insertSublayer:previewLayer below:[[self.view.layer sublayers] objectAtIndex:0]];

[self.view addSubview:[self buildOverlayView]];
}

//--------------------------------------------------------------------------
Expand All @@ -929,6 +918,18 @@ - (void)viewWillAppear:(BOOL)animated {

//--------------------------------------------------------------------------
- (void)viewDidAppear:(BOOL)animated {
// setup capture preview layer
AVCaptureVideoPreviewLayer* previewLayer = self.processor.previewLayer;
previewLayer.frame = self.view.bounds;
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

if ([previewLayer.connection isVideoOrientationSupported]) {
[previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
}

[self.view.layer insertSublayer:previewLayer below:[[self.view.layer sublayers] objectAtIndex:0]];

[self.view addSubview:[self buildOverlayView]];
[self startCapturing];

[super viewDidAppear:animated];
Expand Down Expand Up @@ -989,16 +990,16 @@ - (UIView*)buildOverlayView {
{
return [self buildOverlayViewFromXib];
}
CGRect bounds = self.view.bounds;
CGRect bounds = self.view.frame;
bounds = CGRectMake(0, 0, bounds.size.width, bounds.size.height);

UIView* overlayView = [[UIView alloc] initWithFrame:bounds];
overlayView.autoresizesSubviews = YES;
overlayView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
overlayView.opaque = NO;

UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
self.toolbar = [[UIToolbar alloc] init];
self.toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;

id cancelButton = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
Expand Down Expand Up @@ -1059,44 +1060,23 @@ - (UIView*)buildOverlayView {
[items insertObject:torchButton atIndex:0];
}
}

toolbar.items = items;

bounds = overlayView.bounds;

[toolbar sizeToFit];
CGFloat toolbarHeight = [toolbar frame].size.height;
CGFloat rootViewHeight = CGRectGetHeight(bounds);
CGFloat rootViewWidth = CGRectGetWidth(bounds);
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
[toolbar setFrame:rectArea];

[overlayView addSubview: toolbar];
self.toolbar.items = items;
[overlayView addSubview: self.toolbar];

UIImage* reticleImage = [self buildReticleImage];
UIView* reticleView = [[[UIImageView alloc] initWithImage:reticleImage] autorelease];
CGFloat minAxis = MIN(rootViewHeight, rootViewWidth);

rectArea = CGRectMake(
(CGFloat) (0.5 * (rootViewWidth - minAxis)),
(CGFloat) (0.5 * (rootViewHeight - minAxis)),
minAxis,
minAxis
);
self.reticleView = [[[UIImageView alloc] initWithImage:reticleImage] autorelease];

[reticleView setFrame:rectArea];

reticleView.opaque = NO;
reticleView.contentMode = UIViewContentModeScaleAspectFit;
reticleView.autoresizingMask = (UIViewAutoresizing) (0
self.reticleView.opaque = NO;
self.reticleView.contentMode = UIViewContentModeScaleAspectFit;
self.reticleView.autoresizingMask = (UIViewAutoresizing) (0
| UIViewAutoresizingFlexibleLeftMargin
| UIViewAutoresizingFlexibleRightMargin
| UIViewAutoresizingFlexibleTopMargin
| UIViewAutoresizingFlexibleBottomMargin)
;

[overlayView addSubview: reticleView];

[overlayView addSubview: self.reticleView];
[self resizeElements];
return overlayView;
}

Expand Down Expand Up @@ -1188,7 +1168,35 @@ - (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orien
}

previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

[self resizeElements];
[UIView setAnimationsEnabled:YES];
}

-(void) resizeElements {
CGRect bounds = self.view.bounds;
if (@available(iOS 11.0, *)) {
bounds = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, self.view.safeAreaLayoutGuide.layoutFrame.size.height+self.view.safeAreaLayoutGuide.layoutFrame.origin.y);
}

[self.toolbar sizeToFit];
CGFloat toolbarHeight = [self.toolbar frame].size.height;
CGFloat rootViewHeight = CGRectGetHeight(bounds);
CGFloat rootViewWidth = CGRectGetWidth(bounds);
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
[self.toolbar setFrame:rectArea];

CGFloat minAxis = MIN(rootViewHeight, rootViewWidth);

rectArea = CGRectMake(
(CGFloat) (0.5 * (rootViewWidth - minAxis)),
(CGFloat) (0.5 * (rootViewHeight - minAxis)),
minAxis,
minAxis
);

[self.reticleView setFrame:rectArea];
self.reticleView.center = CGPointMake(self.view.center.x, self.view.center.y-self.toolbar.frame.size.height/2);
}

@end