Swift Modal View Controller with transparent background
You can do it like this:
In your main view controller:
func showModal() {
let modalViewController = ModalViewController()
modalViewController.modalPresentationStyle = .overCurrentContext
presentViewController(modalViewController, animated: true, completion: nil)
}
In your modal view controller:
class ModalViewController: UIViewController {
override func viewDidLoad() {
view.backgroundColor = UIColor.clearColor()
view.opaque = false
}
}
If you are working with a storyboard:
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
- Presentation = Over Current Context
As Crashalot pointed out in his comment: Make sure the segue only uses Default
for both Presentation
and Transition
. Using Current Context
for Presentation
makes the modal turn black instead of remaining transparent.
iOS: Modal ViewController with transparent background
This following code only works on the iPad.
self.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:modalVC animated:YES];
I would go with adding a sub view.
Here is a very good discussion. Look at the comments specifically. Not only the answer.
Modal View
If I were you I wouldn't do it. I would add a sub view and do it. It seems to give me a better control over things.
EDIT:
As mentioned by Paul Linsay, since iOS 8 all that's needed is UIModalPresentationOverFullScreen
for the modalPresentationStyle of the ViewController being presented. This would also cover of navigationBar and tabBar buttons.
Transparent background for modally presented viewcontroller
Fixed it.
The problem was that presentViewController does not keep the view that I was covering.
viewController.modalPresentationStyle = .overCurrentContext
did the trick.
Swift Modal View Controller with transparent background going beyond tab bar
It worked via vc.modalPresentationStyle = .overFullScreen
Present UIViewController as a modal with transparent background
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
How to use presentModalViewController to create a transparent view
Your view is still transparent, but once your modal controller is at the top of the stack, the view behind it is hidden (as is the case with any top-most view controller). The solution is to manually animate a view yourself; then the behind-viewController won't be hidden (since you won't have 'left' it).
Presenting a UIViewController with background transparent in swift 3.0
You just have to present your second Viewcontroller, which has a transparent background, modally. with the following config:
and that's the result :
for those who may not familiar with Opacity: in the situation like this, for transparency, it's better to use Opacity instead of Alpha
Related Topics
Firebase .Indexon Dynamic Keys
How to Load a Url Link That Is Inside a Web View and Keep It in That Web View in Swift
How to Access the Real 100Vh on iOS in CSS
Coca Pod Chart Not Appearing (Swift4)
If No Table View Results, Display "No Results" on Screen
What #Defines Are Set Up by Xcode When Compiling for Iphone
Uicollectionview Auto Scroll to Cell at Indexpath
Converting Utc Date Format to Local Nsdate
Setting Scroll Position in Uitableview
iOS 7 - How to Disable the Swipe Back and Forward Functionality in Safari
Swift - Image Data from Ciimage Qr Code/How to Render Cifilter Output
Upload Files to Dropbox from iOS App with Swift
Font Sizes in Uiwebview Does Not Match iOS Font Size
Get Random Child from Firebase Database
Ask for User Permission to Receive Uilocalnotifications in iOS 8
Silent Push Notifications Only Delivered If Device Is Charging And/Or App Is Foreground
After Switching to Xcode 7, App Size Grew from 9 Mb to 60 Mb, Is There a Fix