将子视图控制器添加到UINavigationController [英] Add child view controller to UINavigationController

查看:215
本文介绍了将子视图控制器添加到UINavigationController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码将子视图控制器添加到 UINavigationController 中包含的 UIViewController

I'm trying to add a child view controller to a UIViewController contained in a UINavigationController with this code:

- (void)buttonTapped:(id)sender
{
    MyChildController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyChild"];
    [self addChildViewController:viewController];
    [self.view addSubview:viewController.view];
    [viewController didMoveToParentViewController:self];


    viewController.view.alpha = 0.0f;
    [UIView animateWithDuration:0.4 animations:^{
        viewController.view.alpha = 1.0f;
    }];
}

但结果如下:

当你可以看到 UINavigatioBar UIToolbar 仍然位于子视图控制器之上。如何将子视图控制器置于最重要的位置?我已经尝试用以下代码替换代码:

As you can see the UINavigatioBar and the UIToolbar are still on top of the child view controller. How can I put the child view controller on top of all? I've already tried to replace the code with:

[self.navigationController addChildViewController:viewController];
    [self.navigationController.view addSubview:viewController.view];
    [viewController didMoveToParentViewController:self.navigationController];

但是这样 viewDidAppear:animated viewController 没有被调用,dunno为什么。

But in this way the viewDidAppear:animated of the viewController doesn't get called, dunno why.

推荐答案

在你的第一个视图控制器中,执行以下操作:

In your first view controller, do something like this:

- (IBAction)buttonClick:(id)sender
{
    SecondViewController *secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    UIImage *blurryImage = [UIImage imageNamed:@"foo.jpeg"];
    secondView.imageView.image = blurryImage;
    [self.navigationController addChildViewController:secondView];
    secondView.view.frame = self.navigationController.view.frame;
    [self.navigationController.view addSubview:secondView.view];
}

然后在你的第二个视图控制器中,为你的imageview添加getter:

Then in your second view controller, add the getter for your imageview:

-(UIImageView *)imageView
{
    if( _imageView == nil )
    {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 548)];
        [self.view addSubview:_imageView];
    }
    return _imageView;
}

这篇关于将子视图控制器添加到UINavigationController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆