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

Commit

Permalink
Use safe areas and reposition elements on rotation (#585)
Browse files Browse the repository at this point in the history
* Use safe areas and reposition elements on rotation

* improved iPhone X toolbar
  • Loading branch information
jcesarmobile authored and macdonst committed Feb 28, 2018
1 parent add72c9 commit 85faa87
Showing 1 changed file with 53 additions and 45 deletions.
98 changes: 53 additions & 45 deletions src/ios/CDVBarcodeScanner.mm
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

0 comments on commit 85faa87

Please sign in to comment.