0
votes

This question has come up before but the accepted solutions don't seem to fix my problem which is why I'm hoping another set of eyes will catch what I'm doing wrong. I'm trying to present a 2nd navigation controller where the background is dark and semi-transparent for an iPhone app so the user can see the contents of the prior view controller. Here's the code:

-(void)viewDidLoad
{
    [super viewDidLoad];
    RootViewController *rvc = [[UIStoryboard storyboardWithName:@"RootView" bundle:nil] instantiateViewControllerWithIdentifier:@"RootViewRVC"];
    self.nvc = [[UINavigationController alloc] initWithRootViewController:rvc];
    self.nvc.modalPresentationStyle = UIModalPresentationOverCurrentContext;    
    [self presentViewController:self.nvc animated:YES completion:nil];
}

The above code yields this:

enter image description here

The app is currently using a storyboard for the navigation controller and rootviewcontroller and displays what I'm trying to achieve:

enter image description here

The TableViewController is a property associated with the rootviewcontroller (the code uses the same storyboard) in both images. From the navigation controller storyboard, the attributes inspector in Xcode 8 has "Presentation" set to "Over Current Context" (while "Over Full Screen" also works) which seems to do the trick, but I prefer to do this programatically so no solutions using storyboards please.

1
Yeah I saw that question as well but for some reason, the accepted solution didn't work for me.Vee
Add definesPresentationContext set as YES. stackoverflow.com/a/26891602/5184217Rajesh Dharani
Rajesh - the link that you sent is also setting modalPresentationStyle to OverCurrentContext which I'm doing prior to pushing the navigation controller. Tried setting definePresentationContext but all it does is shift the navbar of the 2nd navigation controller below the original; doesn't fix the transparent problem that I can't resolve.Vee
Try below one. UIColor *color = ...; color = [color colorWithAlphaComponent:0.5f]; [self.nvc.view setBackgroundColor:color];Rajesh Dharani

1 Answers

0
votes

This may work:

-(void)viewDidLoad
{
    [super viewDidLoad];
    RootViewController *rvc = [[UIStoryboard storyboardWithName:@"RootView" bundle:nil] instantiateViewControllerWithIdentifier:@"RootViewRVC"];
    rvc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    self.nvc = [[UINavigationController alloc] initWithRootViewController:rvc];    
    [self.nvc.view setBackgroundColor:[UIColor colorWithRed:1.0 green:0 blue:0 alpha:0.5]];
    [self presentViewController:self.nvc animated:YES completion:nil];
}