代码:
视图控制器(在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
}
可以在子视图中显示一个摄影机,而不是主视图,前提是视图层次结构的一部分。但是,我发现在此过程中的早期层次结构中必须将其放置在视图层次结构中,以便将摄像机层分配到视图时,视图已被布置,以便它们正确显示。 than试图在尚未列出视图时像上述代码一样试图以编程方式创建视图,我能够通过在情节板中创建视图,对其进行约束,然后将其iboutlet赋予实例变量来使其起作用。然后,当需要显示相机预览时,将摄像机层分配给实例变量。
简而言之,对我有用的是:在情节板上创建Uiview并给予约束。
在View Controller中从Uiview到实例变量创建了一个插座。将摄像机层签名到Uiview的实例变量。