Transparent Background with a Modal UIViewController

25,408

Solution 1

Funny, I was just doing the same thing yesterday. Unfortunately it seems to be impossible. Once the modal view controller is in place, the previous view becomes hidden. See this previous question on the topic.

You can still use the view controller and NIB files you have set up - here's my sample code

- (void)showUpgrade {
    [self.upgradeVC viewWillAppear:NO];
    [self.view addSubview:self.upgradeVC.view];
    [self.upgradeVC viewDidAppear:NO];
}

- (void)hideUpgrade {
    [self.upgradeVC viewWillDisappear:NO];
    [self.upgradeVC.view removeFromSuperview];
    [self.upgradeVC viewDidDisappear:NO];
}

- (UpgradeViewController *)upgradeVC {
    if (_upgradeVC == nil) {
        _upgradeVC = [[UpgradeViewController alloc] initWithNibName:[NSString stringWithFormat:@"UpgradeView_%@", self.deviceType] bundle:nil];
        _upgradeVC.delegate = self;
    }
    return _upgradeVC;
}

You will need to store a reference to the parent view controller in the modal view controller so that you can access the -hide method. I did this through a delegate.

It would also be easy to add some animation to -show and -hide if you want it to animate up from the bottom of the screen - I was just too lazy to do this.

Solution 2

iOS 8 added the UIModalPresentationOverFullScreen presentation style. Set this as the presented view controller’s modalPresentationStyle. For more advanced needs, look into creating a custom presentation controller.

Solution 3

There is now a way to achieve this using iOS7 custom transitions :

MyController * controller = [MyController new];
[controller setTransitioningDelegate:self.transitionController];
controller.modalPresentationStyle = UIModalPresentationCustom;
[self controller animated:YES completion:nil];

To create your custom transition, you need 2 things :

  • A TransitionDelegate object (implementing <UIViewControllerTransitionDelegate>)
  • An "AnimatedTransitioning" object (implementing <UIViewControllerAnimatedTransitioning>)

You can find more informations on custom transitions in this tutorial : http://www.doubleencore.com/2013/09/ios-7-custom-transitions/

Share:
25,408
Mick Walker
Author by

Mick Walker

Updated on August 25, 2020

Comments

  • Mick Walker
    Mick Walker over 3 years

    I have a dilema, I want to present to the user a semi-transparent view.

    I found out by experimenting that if I simply pushed the transparent view to the top of my NavigationController's stack, that it would not render the transparency level I wanted. So I decided to simply add the view as a subview of the current view at the top of the stack.

    This solution works, the view below is still visible, and the View is 'semi-modal'. The problem is, if the parent view inherits from UITableViewController (as mine does), then the view I 'push' onto it, does not cover the navigation bar at the top.

    I really don't want to get into a situation where I am forced to enable / disable controls on the navigation bar every time I push this view, so I was wondering, if anyone knew of any solutions that I could use so that the view I push onto the UITableViewController will actually 'push over' the navigation bar?

  • pix0r
    pix0r about 14 years
    I did not try this myself, but my understanding is that the parent view is actually removed from the view stack. See: stackoverflow.com/questions/587681/…
  • Edward Ashak
    Edward Ashak almost 13 years
    I dont think apple sdk allows a transparent bg on a UIViewController on the iPhone.
  • Admin
    Admin over 11 years
    How do you handle the different orientations of the device when doing this? It seems to me that it will always load the portrait view which is an issue if the current view is in landscape.
  • tooluser
    tooluser over 10 years
    This does not work, for the reasons described - the background views are removed after modal presentation.
  • hackerinheels
    hackerinheels almost 10 years
    this is not a good practice. Look at this article. It talks about why adding one VC's view to another as a subview is a bad idea. blog.carbonfive.com/2011/03/09/abusing-uiviewcontrollers
  • Johnny Rockex
    Johnny Rockex almost 10 years
    Possible to take a snapshot of the (static) view underneath and apply as background?
  • Manuel
    Manuel over 8 years
    This should be the correct answer now that iOS 8+ has an adoption of > 91% on iOS devices as of Oct 24, 2015.