iphone - 将 subview Controller 添加到 UINavigationController

标签 iphone objective-c uiviewcontroller ios7 childviewcontroller

我正在尝试使用以下代码将 subview Controller 添加到包含在 UINavigationController 中的 UIViewController 中:

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

但这是结果:

Image Result

如您所见,UINavigatioBarUIToolbar 仍在 subview Controller 之上。我怎样才能把 subview Controller 放在最上面?我已经尝试用以下代码替换代码:

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

但是这样 viewControllerviewDidAppear:animated 不会被调用。我不知道,为什么。

最佳答案

@Sam的评论是正确的。您需要调用 beginApperanceTransition:animated:endAppearanceTransition 来触发 viewDidAppearUINavigationController之所以在添加 subview Controller 时不调用viewDidAppear,是因为它重写了其容器组合方法,以防止程序员以奇怪的方式添加 subview Controller 地方。在您的情况下,它不希望您的 subview 覆盖导航栏。导航 Controller 的正确用法是让子项出现在导航栏下方。尽管如此,您仍然可以通过手动告诉 child 何时出现以及何时结束出现来强制使用此非标准 UI。

给 UINavigationController 添加一个 child

MyChildViewController* child = [[MyChildViewController alloc] init];
[self.navigationController addChildViewController:child];
child.view.frame = self.navigationController.view.bounds;
[self.navigationController.view addSubview: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];
    }
];

从 UINavigationController 中移除一个 child

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

关于iphone - 将 subview Controller 添加到 UINavigationController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19257999/

相关文章:

ios - NSDateFormatter 上午/下午没有空格

ios - View 未填充数据 AFNetworking

iphone - NSURL 为 null 而 NSString 在 Objective-C 中是正确的

objective-c - 了解应用程序是否在测试环境中运行

objective-c - SMJobBless 导致 kSMErrorDomainFramework 错误 5 - 指定路径的工具无效

ios - 如何以较小的高度在当前 View Controller 的顶部呈现 UIViewController

iphone - 如何实现多个下载代码 objective-c

iphone - 体系结构 i386 的重复符号,找不到相同的类名和文件名

ios - 如何重置初始 ViewController 的状态?

ios - 模态关闭后 viewWillAppear 没有更新旋转