18
votes

I'm trying to present a viewcontroller with a transparent background on both iOS 7 and iOS 8. Just by changing the viewcontroller's modalPresentationStyle property to FormSheet I can get it working on iOS 7.1.

What I want is a universal way to that on ios7+

I have tried using other options to modalPresentationStyle, like: OverCurrentContext, CurrentContext and PageSheet.

I also tried to use the modalPresentationStyle.Custom but didnt have any success.

I have NavigationController if that helps in anything.

The code for the presenting view controller:

InfoViewController *info = [[InfoViewController alloc] initWithNibName:@"InfoViewController" bundle:nil];
[self presentViewController:info animated:YES completion:nil];

And the code for the viewDidLoad(which I think has a relevant part on this) of the presented ViewController:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    self.modalPresentationStyle = UIModalPresentationStyle.PageSheet
}

I´m using swift and Xcode 6. Here´s a screenshot of what I have now and of what I want, respectively:

enter image description hereenter image description here

Here's an example code: https://github.com/pbassut/TransBackgroundViewController

8
can you post to github so we can play with the code?Steve Rosenberg
you mean the entire code?Patrick Bassut
Just the relevant part that is not working so we can paste it on a new project and see the failure and make corrections.Steve Rosenberg
I'm confused. The project is loading a xib with a black background on top of view with a green background.Steve Rosenberg

8 Answers

21
votes

For those still with this problem before presenting the UIViewController set the modalPresentationStyle of the presented UIViewController to .Custom and it will work on iOS 8(Xcode 6.1). That is, you should set it in the presenting UIViewController

13
votes

I've tried this solution and it works on both iOS 7 and 8:

if (UIDevice.currentDevice().systemVersion.integerValue >= 8)
{
    //For iOS 8
    presentingViewController.providesPresentationContextTransitionStyle = true;
    presentingViewController.definesPresentationContext = true;
    presentedViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
}
else
{
    //For iOS 7
    presentingViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
}

Note: Be aware of the difference between 'presentingViewController' and 'presentedViewController'.

8
votes

None of the above worked for me. To get this working on iOS 10, I simply: presented.modalPresentationStyle = .overFullScreen presenting.present(presented, animated: true) Then set the presented view controller's root view to have a transparent or semi transparent color. Don't change its alpha or all of its subviews will have the same alpha.

7
votes

I was able to achieve this with no code:

  1. In the storyboard, on the presenting view controller (i.e. before you segue to the one you want to be transparent), go to the attributes inspector. On that tab, there are checkboxes for "Defines Context" and "Provides Context". Check those.

  2. On the segue, set it to be "Present Modally".

  3. On the destination/presented view controller, go to the attributes tab. In the "Presentation" drop down, select "Over Current Context".

  4. On the destination view controller, set it's view to have a clear background.

  5. On the destination view controller, slap down 2 UIViews: One is your "transparent" one, and the other is your "content" one. Set the transparent one to have whatever alpha you like and put all your junk in the "content" one.

4
votes

I achieve transparent with various styles in Swift 2.0 by following below steps. Analyse it to compatible with your code.

The transparent view controller(modal view controller) is launched by segue when a button click on main view controller.

Segue Settings:

enter image description here

In Main view controller:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
{
 let vc = segue.destinationViewController as! ModalViewController
 vc.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext //All objects and view are transparent
}

Modal View Controller on StoryBoard:

  1. If you need view and all objects are transparent

enter image description here

  1. If you need view only transparent and over the objects are not transparent

enter image description here

On your Modal View Controller

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.8) //view only black coloured transparent

}

Sample Screenshots:

  1. View and over the objects are transparent

enter image description here

  1. View only transparent

enter image description here

For any other queries please comment here :)

If you confuse or more about with transparent view controller then watch my video tutorial for you on Youtube :D

2
votes

iOS8+

Below code snippet will solve this problem in iOS8+

SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;           
[self presentViewController:secondViewController animated:YES completion:nil];    
0
votes

I had a similar problem, i wanted to do something similar to the second screenshot that you present here (the brazilian/portuguese newspaper!)

I solved it like this. I inserted a UIView that covered the entire View Controller, then i went to the Attributes Inspector and i set the UIView's background color to black, with 50% opacity. Then, i inserted another UIView above the first one, and i worked on that one

0
votes

If you change the view alpha, it will be applied to its subview, as it understands that the whole view is transparent. The easy solution is change its colour with less opacity. Just select your background colour to custom

selecting background colour

then select the RGB slider, choose your colour, and reduce the opacity.

colour selection