0

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.

Vee
  • 1,821
  • 3
  • 36
  • 60
  • Possible duplicate of [Transparent background for modally presented viewcontroller](http://stackoverflow.com/questions/27669699/transparent-background-for-modally-presented-viewcontroller) – dmorrow Mar 20 '17 at 22:46
  • Yeah I saw that question as well but for some reason, the accepted solution didn't work for me. – Vee Mar 21 '17 at 00:30
  • Add definesPresentationContext set as YES. http://stackoverflow.com/a/26891602/5184217 – Rajesh Dharani Mar 21 '17 at 02:03
  • 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 Mar 21 '17 at 02:21
  • Try below one. UIColor *color = ...; color = [color colorWithAlphaComponent:0.5f]; [self.nvc.view setBackgroundColor:color]; – Rajesh Dharani Mar 21 '17 at 06:12
  • Can you try to present the view controller in `viewDidAppear` or later? `viewDidLoad` is trigger before the presenting view controller is on the view stack. – dmorrow Mar 21 '17 at 13:07

1 Answers1

0

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];
}
Allan Poole
  • 378
  • 1
  • 10