Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS : ModalView with background transparent?

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,

like image 412
deveLost Avatar asked Feb 13 '14 17:02

deveLost


1 Answers

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.

like image 64
Pankaj Wadhwa Avatar answered Sep 21 '22 19:09

Pankaj Wadhwa