This question is about displaying a camera view in a subview as opposed to the main self.view.
According to AI, it should be possible to display it in a subview as long as the subview is in the view hierarchy, but I can't make this work. I'm wondering if there is somethign special you need to do with respect to the Z index of the subview, its alpha value or something else.
Is it true as various AIs say that you can display a cameraView through a subView as well as through the main view? If so what other steps beyond assigning the subView a place in the hierarchy are needed?
Code:
View Controller (in Obj-c)
- (void)viewDidLoad {
[super viewDidLoad];
UIView *aView = [UIView new];
[self.view addSubview: aView];//have tried this and following
//[self.view insertSubview:aView atIndex:0];
//[self.view bringSubviewToFront:aView];//also tried this
aView.translatesAutoresizingMaskIntoConstraints = false;
float height = self.previewView.frame.size.height;
float width = self.previewView.frame.size.width;
aView.alpha = 0.1;
[aView.widthAnchor constraintEqualToConstant:width].active = YES;
[aView.heightAnchor constraintEqualToConstant:height].active = YES;
[aView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = YES;
[aView.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:25].active = YES;
// [[Utilities shared] showLivePreviewWithView:self.view];//works
[[Utilities shared] showLivePreviewWithView:aView];//does not work
}
This question is about displaying a camera view in a subview as opposed to the main self.view.
According to AI, it should be possible to display it in a subview as long as the subview is in the view hierarchy, but I can't make this work. I'm wondering if there is somethign special you need to do with respect to the Z index of the subview, its alpha value or something else.
Is it true as various AIs say that you can display a cameraView through a subView as well as through the main view? If so what other steps beyond assigning the subView a place in the hierarchy are needed?
Code:
View Controller (in Obj-c)
- (void)viewDidLoad {
[super viewDidLoad];
UIView *aView = [UIView new];
[self.view addSubview: aView];//have tried this and following
//[self.view insertSubview:aView atIndex:0];
//[self.view bringSubviewToFront:aView];//also tried this
aView.translatesAutoresizingMaskIntoConstraints = false;
float height = self.previewView.frame.size.height;
float width = self.previewView.frame.size.width;
aView.alpha = 0.1;
[aView.widthAnchor constraintEqualToConstant:width].active = YES;
[aView.heightAnchor constraintEqualToConstant:height].active = YES;
[aView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = YES;
[aView.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:25].active = YES;
// [[Utilities shared] showLivePreviewWithView:self.view];//works
[[Utilities shared] showLivePreviewWithView:aView];//does not work
}
Share
edited Mar 25 at 22:58
halfer
20.4k19 gold badges109 silver badges202 bronze badges
asked Mar 9 at 0:29
user6631314user6631314
1,9801 gold badge18 silver badges60 bronze badges
6
|
Show 1 more comment
1 Answer
Reset to default 0It is possible to display a cameraview in a subview as opposed to the main view providing it is part of the view hierarchy. However, I found it necessary place it in the view hierarchy earlier in the process so that when you assign the camera layer to the the view the views have been laid out so that they display properly.
Rather than trying to create the view programmatically right before attaching the camera view as in the above code when the views were not yet laid out, I was able to get it to work by creating the view In Storyboard, giving it constraints and then giving it an IBOutlet to an instance variable. Then when it comes time to show the camera preview, assign the camera layer to the instance variable.
In short what worked for me was:
Create UIView in Storyboard and give it constraints.
Create an outlet from the UIView to an instance variable in the View Controller.
Assign the camera layer to the instance variable of the UIView.
showLivePreview
function which you show in the other question. You'll get much better help having one complete question instead of two partial but related questions. – HangarRash Commented Mar 9 at 4:11