dev.nikolaz
dev.nikolaz

Reputation: 3184

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

Upvotes: 7

Views: 5771

Answers (3)

OhadM
OhadM

Reputation: 4803

You basically need to tell the navigation controller to:

navigation.modalPresentationStyle = .overCurrentContext

In other words:

A presentation style where the content is displayed over another view controller’s content.

and that's it.

You can also make sure that:

navigation.view.backgroundColor = .clear

Upvotes: 1

malex
malex

Reputation: 10096

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

Upvotes: 2

Vadoff
Vadoff

Reputation: 9419

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).

Upvotes: 2

Related Questions