Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationController clear background color

That simple example but that don't work;

I have ViewController where inside on NavigationConroller, then I want to add new ViewConroller with its self navigation controller.

In main viewController:

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

[self presentViewController:nav animated:NO completion:nil];

Two controllers has a background color clear, but still black color. Navigation bar I can do clear, but not a view.

UPDATE:

if i change self.window.backroundColor to red for example, that work but not clear

UPDATE 2:

[self addChildViewController:vc];  
[self.view addSubview:vc.view];
[vc didMoveToParentViewController:self];

and when I want to dealloc vc

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

All work ok without navigation controller

like image 733
dev.nikolaz Avatar asked Aug 12 '14 20:08

dev.nikolaz


2 Answers

A viewController's view's backgroundColor can't be clear (as in showing the previous viewController's view on the stack). Pushing or presenting a viewController will put the new viewController on the stack and hide the previous viewController completely.

If you want a clear backgroundColor on the view, you will need to either:

1) set the viewController as a childViewController of the previous viewController - then animate the transition yourself.

Or

2) transplant the viewController logic into the previous viewController and have a new uiview act as that view (you also need to animated the transition yourself).

like image 169
Vadoff Avatar answered Sep 29 '22 22:09

Vadoff


The solution is as follows. For clear example we use tableViewController:

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

The key feature is that you have to set modalPresentationStyle of presentingViewController to UIModalPresentationCurrentContext.

It works fine BUT without slide animation. You will get result immediately. But you can still use "blood hack" to retain visual animation by successive presenting, dismissing and presenting again:

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];
    }];
}];
like image 34
malex Avatar answered Sep 30 '22 00:09

malex