You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS开发:子视图显示cameraView失败,求可行性验证及解决方案

在子视图中显示相机预览视图的问题

我尝试在自定义子视图而非主视图self.view中显示相机预览视图(cameraView),但实际操作无法生效。此前有说法提到只要子视图处于视图层级内即可实现,我对此有两个疑问:

  1. 「子视图与主视图均可显示cameraView」这个说法是否属实?
  2. 如果属实,除了将子视图加入视图层级外,还需要哪些步骤才能让相机预览在子视图中正常显示?另外是否需要对子视图的Z索引、alpha值等属性进行特殊设置?

视图控制器代码(Objective-C)

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *aView = [UIView new];
    [self.view addSubview: aView];//尝试过这行及下面的代码
    //[self.view insertSubview:aView atIndex:0];
    //[self.view bringSubviewToFront:aView];//也尝试过这行
    
    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];//这行代码可以正常显示
    [[Utilities shared] showLivePreviewWithView:aView];//这行代码无法生效
}

内容的提问来源于stack exchange,提问作者user6631314

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 01:12:35