Add child view controller to UINavigationController

http://stackoverflow.com/questions/19257999/add-child-view-controller-to-uinavigationcontroller

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;
    }];
}

But this is the result:

Image Result

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];

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

share improve this question
 

2 Answers

up vote 5 down vote accepted

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];
}

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;
}
share improve this answer
 
 
I'm trying to use the viewcontroller-container approach because I need to blur the background view of the modal view controller that is also semi-transparent. The effect is the same as notification view that you pull down from the top of the screen. The background is semi-transparent and it's also blur/frosted glass. Why if I add a child view controller to self.navigationController the viewDidAppear method is not called? –  Fred Collins  Oct 8 '13 at 23:21
 
Hmm...not quite sure...have you tried calling [viewController viewWillAppear:NO]? –  JonahGabriel  Oct 8 '13 at 23:38
 
Yes and it does nothing. I need to call [viewController viewDidAppear:NO] just after the line[viewController didMoveToParentViewController:self.navigationController]. Why the hell the methods are not called? Otherwise if I add the child view controller to self and notself.navigationController it works but as showed in the above image the view is 'inside' thenavigationController. –  Fred Collins  Oct 8 '13 at 23:44
 
So I just coded up a simple case of your problem and it looks like viewDidLoad is getting called. Why not put your logic in there? I am imagining you are deallocating your view when it gets hidden right? –  JonahGabriel Oct 8 '13 at 23:51
3 
In order for viewWillAppear: and viewDidAppear: to be called, you need to send the message -beginAppearanceTransition:animated: to the view controller responsible for the view you added. Then you do the animation, if any. And in the animation completion block, send -endAppearanceTransition to the view controller that appeared. NOTE: Do not call [viewController viewDidAppear:NO] - you should never invoke these methods yourself. –  Sam  Nov 12 '13 at 10:41

@Sam's comment is correct. You need to call beginApperanceTransition:animated: and endAppearanceTransition for viewDidAppear to be triggered. The reason why UINavigationController does not call viewDidAppear when you add a child view controller is because it has overridden its container composition methods to prevent the programmer from adding a child view controller in strange places. In your case, it doesn't want your child view to cover up the navigation bar. The correct usage of a navigation controller is to have children appear under the navigation bar. Nonetheless, you can still force upon this non-standard UI by manually telling the child when it is appearing and when it has finished appearing.

Add a child to UINavigationController

MyChildViewController* child = [[MyChildViewController alloc] init]l
[self.navigationController addChildViewController:child];
child.view.frame = self.navigationController.bounds;
[self.navigationController.view addSubjvew:child.view];
child.view.alpha = 0.0;
[child beginAppearanceTransition:YES animated:YES];
[UIView
    animateWithDuration:0.3
    delay:0.0
    options:UIViewAnimationOptionCurveEaseOut
    animations:^(void){
        child.view.alpha = 1.0;
    }
    completion:^(BOOL finished) {
        [child endAppearanceTransition];
        [child didMoveToParentViewController:self.navigationController];
    }
];

Remove a child from UINavigationController

[child willMoveToParentViewController:nil];
[child beginAppearanceTransition:NO animated:YES];
[UIView
    animateWithDuration:0.3
    delay:0.0
     options:UIViewAnimationOptionCurveEaseOut
    animations:^(void){
        child.view.alpha = 0.0;
    }
    completion:^(BOOL finished) {
        [child endAppearanceTransition];
        [child.view removeFromSuperview];
        [child removeFromParentViewController];
    }
];
share improve this answer
 
1 
I think child be calling didMoveToParentViewController: when its being removed.. Something like,[child didMoveToParentViewController:nil] –  Mohammad Abdurraafay  May 11 '14 at 8:37
 
It helped me ... thank you. Gave upvote Pwner. –  Meenu  Jul 29 '14 at 12:43
 
very helpful, thanks! –  kernix  Jul 26 at 12:48
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值