如何在复杂的视图层次结构中呈现相机

Posted

技术标签:

【中文标题】如何在复杂的视图层次结构中呈现相机【英文标题】:How to present a camera in complicated view hierarchy 【发布时间】:2018-02-27 10:15:10 【问题描述】:

ios 10.3.1

我有以下视图控制器层次结构...

<TabBarController 0x1463d550>, state: disappeared, view: <UIView 0x146434b0> not in the window
   | <TabMapController 0x146396f0>, state: disappeared, view: <UIView 0x16168be0> not in the window
   |    | <ControlBar 0x1619ad60>, state: disappeared, view: <UIToolbar 0x161b6d40> not in the window
   |    | <ArcGisViewController 0x14d7a200>, state: disappeared, view: <UIView 0x162cd2c0> not in the window
   |    | <iRpMainDashboardViewController 0x14dc1a00>, state: disappeared, view: <UIView 0x16553d60> not in the window
   |    |    | <iRpImageFlipperViewController 0x16615910>, state: disappeared, view: <IGGridView 0x14dfec00> not in the window
   |    |    | <iRpImageFlipperViewController 0x165567e0>, state: disappeared, view: <IGGridView 0x14e31e00> not in the window
   + <UINavigationController 0x14a13800>, state: appeared, view: <UILayoutContainerView 0x16282410>, presented with: <_UIFullscreenPresentationController 0x16cad680>
   |    | <TabMobile 0x15a53400>, state: appeared, view: <UIView 0x162844a0>
   |    |    | <TabMobileDetail 0x16ca63c0>, state: appeared, view: <BaseView 0x162f4020>
   |    |    |    | <MobileAccount 0x16ca8440>, state: appeared, view: <UIView 0x16ca9030>
   |    |    |    |    | <ComboBoxController 0x16cd2510>, state: appeared, view: <ComboBoxView 0x16cce9a0>
   |    |    |    |    | <ComboBoxController 0x16cab940>, state: appeared, view: <ComboBoxView 0x16cd15c0>
   |    |    |    | <MobileCharacteristic 0x16cd2840>, state: appeared, view: <UIView 0x16cd2b60>
   |    |    |    |    | <ComboBoxController 0x16fb7630>, state: appeared, view: <ComboBoxView 0x16d05da0>
   |    |    |    |    | <ComboBoxController 0x16fb9300>, state: appeared, view: <ComboBoxView 0x16d05fc0>
   |    |    |    |    | <ComboBoxController 0x16fb77f0>, state: appeared, view: <ComboBoxView 0x16d060d0>
   |    |    |    |    | <ComboBoxController 0x16fb79b0>, state: appeared, view: <ComboBoxView 0x16d05eb0>
   |    |    |    | <MobileLocation 0x16fb7b70>, state: appeared, view: <UIView 0x16d1ca60>
   |    |    |    |    | <ComboBoxController 0x16d065c0>, state: appeared, view: <ComboBoxView 0x16d21530>
   |    |    |    |    | <ComboBoxController 0x16d2a2f0>, state: appeared, view: <ComboBoxView 0x16d21790>
   |    |    |    | <AxGridPictController 0x16d25b00>, state: appeared, view: <UIView 0x165b2730>
   |    |    | <GridControlBar 0x16aeeff0>, state: appeared, view: <GridControlBarView 0x16d3ddf0>

AxGridPictController(见层次结构底部附近)上有一个相机按钮。

当用户点击 AxGridPictController 上的相机按钮时,我在 AppDelegate 中有代码尝试显示相机。

我在我的 AppDelegate 中向相机展示此代码...

-(void)presentCameraToUser
              
        //Create observer for notification
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(performTask:) name:@"ImageCapture" object:nil];

        //open camera view controller
        self.cameraViewController = [[AVCamCameraViewController alloc] initWithNibName:@"AVCamCameraViewController" bundle:nil];

        [self.tabBarController presentViewController:self.cameraViewController animated:YES completion:nil];
    

我没有从 AppDelegate 引用我的 UINavigationController,因为 UINavigationController 完全来自另一个类。我的摄像头代码集中在 AppDelegate 类中,因此我的任何一个类都可以使用摄像头。

您会注意到我的演示代码是[self.tabBarController presentViewController... ],但根据视图控制器层次结构您还会注意到,一旦我演示导航控制器,tabBarController 就会有state: disappearedview: not in the window,所以我我假设我尝试使用tabBarController 来展示相机是没有用的。

假设我什至可以获得对 UINavigationController 的引用,我什至不确定导航控制器是否能够呈现全屏相机视图。

我正在尝试做什么...在导航控制器处于活动状态时显示相机...甚至可能吗?

【问题讨论】:

【参考方案1】:

解决方案非常简单。我没有使用父视图控制器来呈现 UINavigationController,而是像这样它添加到父视图中...

-(void)addAChildViewController:(UIViewController *)childViewController
    
        [self addChildViewController:childViewController];
        [self.view addSubview:childViewController.view];
        [childViewController didMoveToParentViewController:self];
    

而不是 dismissing UINavigationController,我只是用这样的代码删除它......

-(void)removeThisViewControllerFromItsParent
    
        [self willMoveToParentViewController:nil];
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    

【讨论】:

以上是关于如何在复杂的视图层次结构中呈现相机的主要内容,如果未能解决你的问题,请参考以下文章

如何解决此错误“警告:尝试呈现其视图不在窗口层次结构中”?

切换标签栏项目时,UINavigationController 如何将其整个视图控制器表示层次结构保存在内存中?

是否可以在 UINavigationController 前面显示相机视图

尝试在其视图不在窗口层次结构中的 UIViewController 上呈现 UIViewController

尝试在其视图不在窗口层次结构中的 UIViewController 上呈现 UIViewController

尝试在其视图不在窗口层次结构中的 UIViewController 上呈现 UIViewController