Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Present UIViewController as a modal with transparent background

Tags:

uikit

swift

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

like image 781
Patrick Bassut Avatar asked Oct 27 '14 22:10

Patrick Bassut


People also ask

How do you make a transparent view in Swift?

Basic Swift Code for iOS AppsView's Alpha value is a floating-point number in the range 0.0 to 1.0, where 0.0 represents totally transparent and 1.0 represents totally opaque. Changing the value of this property updates the alpha value of the current view only.

How do I make my view controller transparent?

Just add a Storyboard Segue with Kind set to Present Modally to your modal view controller and on this view controller set the following values: Background = Clear Color. Drawing = Uncheck the Opaque checkbox.

What is a UIViewController?

The UIViewController class defines the methods and properties for managing your views, handling events, transitioning from one view controller to another, and coordinating with other parts of your app.


1 Answers

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

like image 59
Patrick Bassut Avatar answered Oct 16 '22 02:10

Patrick Bassut