Ios UINavigationController清晰背景色

Ios UINavigationController清晰背景色,ios,uiviewcontroller,uinavigationcontroller,Ios,Uiviewcontroller,Uinavigationcontroller,那个简单的例子,但不起作用 我在NavigationConroller中有ViewController,然后我想添加新的ViewConroller及其自导航控制器 在主视图控制器中: CustomViewController *vc = [[CustomViewController alloc] init]; NewNavigationVC *nav = [[NewNavigationVC alloc] initWithRootViewController:vc]; [self present

那个简单的例子,但不起作用

我在NavigationConroller中有ViewController,然后我想添加新的ViewConroller及其自导航控制器

在主视图控制器中:

CustomViewController *vc = [[CustomViewController alloc] init];
NewNavigationVC *nav = [[NewNavigationVC alloc] initWithRootViewController:vc];

[self presentViewController:nav animated:NO completion:nil];
UITableViewController *modalVC = [UITableViewController new];
UINavigationController *modalNVC = [[UINavigationController alloc] initWithRootViewController:modalVC];

UIViewController *mainVC = [UIViewController new];
UINavigationController *mainNVC = [[UINavigationController alloc] initWithRootViewController:mainVC];

modalVC.view.backgroundColor = UIColor.clearColor;
mainVC.view.backgroundColor = UIColor.redColor;
mainNVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[mainNVC presentViewController:modalNVC animated:YES completion:NULL];
两个控制器的背景色清晰,但仍为黑色。 导航栏我可以做清楚,但不是一个视图

更新:

[self addChildViewController:vc];  
[self.view addSubview:vc.view];
[vc didMoveToParentViewController:self];
例如,如果我将self.window.backroundColor更改为红色,则该选项有效,但不清晰

更新2:

[self addChildViewController:vc];  
[self.view addSubview:vc.view];
[vc didMoveToParentViewController:self];
当我想放弃时

[vc willMoveToParentViewController:nil];
[vc.view removeFromSuperview];
[vc removeFromParentViewController];

如果没有导航控制器,一切正常

viewController视图的背景色无法清晰显示(如在堆栈上显示上一个viewController的视图)。按下或显示viewController会将新的viewController放在堆栈上,并完全隐藏以前的viewController

如果希望视图上有清晰的背景色,则需要:

1) 将viewController设置为上一个viewController的childViewController-然后自己制作过渡动画


2) 将viewController逻辑移植到以前的viewController中,并使用新的uiview作为该视图(您还需要自己设置转换动画)。

解决方案如下。例如,我们使用tableViewController:

CustomViewController *vc = [[CustomViewController alloc] init];
NewNavigationVC *nav = [[NewNavigationVC alloc] initWithRootViewController:vc];

[self presentViewController:nav animated:NO completion:nil];
UITableViewController *modalVC = [UITableViewController new];
UINavigationController *modalNVC = [[UINavigationController alloc] initWithRootViewController:modalVC];

UIViewController *mainVC = [UIViewController new];
UINavigationController *mainNVC = [[UINavigationController alloc] initWithRootViewController:mainVC];

modalVC.view.backgroundColor = UIColor.clearColor;
mainVC.view.backgroundColor = UIColor.redColor;
mainNVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[mainNVC presentViewController:modalNVC animated:YES completion:NULL];
关键功能是您必须将presentingViewController的
modalPresentationStyle
设置为
UIModalPresentationCurrentContext

它工作正常,但没有幻灯片动画。你会马上得到结果的。 但是,您仍然可以使用“血色黑客”通过连续呈现、驳回和再次呈现来保留视觉动画:

modalVC.view.backgroundColor = UIColor.clearColor;
mainVC.view.backgroundColor = UIColor.redColor;

[mainNVC presentViewController:modalNVC animated:YES completion:^{
    [modalNVC dismissViewControllerAnimated:NO completion:^{
        mainNVC.modalPresentationStyle = UIModalPresentationCurrentContext;
        [mainNVC presentViewController:modalNVC animated:NO completion:NULL];
    }];
}];

您基本上需要告诉导航控制器:

换言之:

一种显示样式,其中内容显示在另一个视图控制器的内容上

就这样

您还可以确保:

navigation.view.backgroundColor = .clear

是的,看起来不错,在添加子视图之前,您需要将vc.view.frame设置为适合搜索栏下方,或者在childViewController的didMoveToParentViewController方法中设置该框架。您可能还想将动画放在didMoveToParentViewController中。@Vadoff您能举出一个来源吗?这听起来不错,但我找不到证明或反驳这一点的文档。您可以使用modalTransitionStyle设置演示转换模式,或者创建自己的演示转换模式。从长远来看,代码的最后一部分将使您面临问题。@PeterSuwara这是克服旧iOS版本错误的非常古老的解决方案。现在,没有第二部分,它已经足够好了。