30

I want to show a modalview on a viewController. (which has a naviguation controller).

On my view i have text, and a button to show the modalview.

I created a .xib which contained my modalview (it's a view with an image and a label).

When i show it, with that :

ShareController *controller =  [[ShareController alloc] initWithNibName:@"ShareController" bundle: nil];
controller.view.backgroundColor = [UIColor clearColor];
controller.modalPresentationStyle = UIModalPresentationFormSheet;
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

[self presentViewController:controller animated:YES completion:nil];

i have my modalview, BUT, the background become black.. and i want to see always the text on my view. (i tried to set alpha, etc..; but NOTHING runs :'( )

Someone to help me ?

Thanks,

deveLost
  • 1,151
  • 2
  • 20
  • 47
  • "when you present modally a view controller (transparent) there is nothing underneath it except the app window, which is black. iOS 7 introduced a new modal presentation style that causes iOS not to remove the views underneath the presented view controller (you must provide a transition delegate)".. you have an example here: https://github.com/hightech/iOS-7-Custom-ModalViewController-Transitions – TonyMkenu Feb 13 '14 at 21:50
  • http://programming.oreilly.com/2014/01/transcending-uialertview-on-ios-7.html – matt Feb 13 '14 at 22:20
  • Check [my answer here](http://stackoverflow.com/a/27175632/817598) for a solution that works in both iOS 7 and 8 – Pedro Mancheno Nov 27 '14 at 16:53

6 Answers6

105

Use following snippet to do it on iOS 8 onwards:

For Objective C:

UIViewController *walkThru = [self.storyboard   instantiateViewControllerWithIdentifier:@"WalkThroughScene"];
walkThru.providesPresentationContextTransitionStyle = YES;
walkThru.definesPresentationContext = YES;
[walkThru setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[self.navigationController presentViewController:walkThru animated:YES completion:nil];

For Swift 2 :

let viewController : XYZViewController =     self.storyboard!.instantiateViewControllerWithIdentifier(“XYZIdentifier”) as! XYZViewController
viewController.providesPresentationContextTransitionStyle = true
viewController.definesPresentationContext = true
viewController.modalPresentationStyle=UIModalPresentationStyle.OverCurrentContext
self.presentViewController(viewController, animated: true, completion: nil)

For Swift 4 :

let viewController =  self.storyboard!.instantiateViewController(withIdentifier:  "XYZIdentifier") as! XYZViewController
viewController.providesPresentationContextTransitionStyle = true
viewController.definesPresentationContext = true
viewController.modalPresentationStyle = .overCurrentContext
self.present(viewController, animated: true, completion: nil)

And the backgroundcolor of your presented viewController should be clearColor.

Pankaj Wadhwa
  • 3,073
  • 3
  • 28
  • 37
25

you can check the iOS7 example (see my comm) or you can simple try this:

remove this line from your "presenting" method

controller.view.backgroundColor = [UIColor clearColor];

now, in viewDidLoad of the ShareController add:

 self.view.backgroundColor = [UIColor clearColor];
 self.modalPresentationStyle = UIModalPresentationCurrentContext;
 self.modalPresentationStyle = UIModalPresentationFormSheet;

PS

if you have a navigationController... use

[self.navigationController presentViewController:controller animated:YES completion:nil];
Hiren
  • 12,720
  • 7
  • 52
  • 72
TonyMkenu
  • 7,597
  • 3
  • 27
  • 49
  • I've just one problem, when i show my modalview, i haven't transition :/ I have juste a vertical transition when i close it. – deveLost Feb 14 '14 at 08:57
  • yes, i know... :( this is just a "hack"; for animation - use @matt's solution or the other example from github – TonyMkenu Feb 14 '14 at 10:02
  • I see there is "UIActivityViewController", we can't use it for a modal view ? (for social sharing) – deveLost Feb 14 '14 at 15:11
  • 14
    This doesnt work for me on iOS 8. The animation works, but after view appears, it's background is back to black. – Legoless Jul 01 '15 at 09:56
3

If you want your modalVC to be over the tabbar you need to define the presentation style as UIModalPresentationOverFullScreen

[_presentedViewController setModalPresentationStyle:UIModalPresentationOverFullScreen];

[_presentingViewController presentViewController:_presentedViewController animated:YES completion:nil];
dorian
  • 847
  • 5
  • 11
2

The quick answer is you cannot present transparent modal view, not with presentViewController:animated:completion: method. Because you cannot make the modal view controller transparent (your view is placer on top of that).

You can make custom view and you can manually animate it, this is a way to create something what you need.

Greg
  • 25,317
  • 6
  • 53
  • 62
  • 1
    It's possible in iOS 7 with custom transitions. – David Snabel-Caunt Feb 13 '14 at 17:14
  • If i understand, like that it's not possible because when i show my modalview, this windows is on the "top", and so, my other view was hidden ? – deveLost Feb 13 '14 at 19:52
  • 1
    You just said it's on the top of presenting view. It means it's possible, when modal animation finished, iOS set presenting view alpha to zero, that's why you get the black background. – Edward Anthony Dec 20 '14 at 02:18
2

In the PresentingViewController copy the below code under storyboard segue or IBAction.

SecondViewController *viewController = [self.storyboard   instantiateViewControllerWithIdentifier:@"secondViewController"];
//SecondViewController *viewController = [SecondViewController alloc]init]; // if you are adding the viewcontroller programatically
viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [viewController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:viewController animated:YES completion:nil];

In the second ViewController do the following steps either by storyboard or through code:

  1. Set the view's (self.view) backgroundcolor to clearcolor and reduce the opacity to 50 %
  2. Now you can realize a semi transparent modal view
0

That is the UIWindow's color,you can init the UIWindow color in appDelegate.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    return YES;
}
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
N.nnd
  • 15
  • 8