Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to present a modal atop the current view in Swift

(Xcode6, iOS8, Swift, iPad)

I am trying to create a classic Web-like modal view, where the outside of the dialog box is "grayed-out." To accomplish this, I've set the alpha value of the backgroundColor of the view for the modal to 0.5, like so:

self.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.5) 

The only problem is that when the modal becomes full-screen, the presenting view is removed. (Ref Transparent Modal View on Navigation Controller).

(A bit irritated at the concept here. Why remove the underlying view? A modal is, by definition, to appear atop other content. Once the underlying view is removed, it's not really a modal anymore. it's somewhere between a modal and a push transition. Wa wa wa... Anyway..)

To prevent this from happening, I've set the modalPresentationStyle to CurrentContext in the viewDidLoad method of the parent controller, and in Storyboard... but no luck.

    self.modalPresentationStyle = UIModalPresentationStyle.CurrentContext     self.navigationController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext 

How do I prevent the presenting view from being removed when the modal becomes full screen?

tyvm.. more info below.

Also in Storyboard, like so (Presentation: Current Context)

enter image description here

Thx for your help... documentation below:

enter image description here

like image 608
kmiklas Avatar asked Jun 20 '14 21:06

kmiklas


People also ask

How do you dismiss a modal view controller?

According to the View Controller Programming guide for iPhone OS, this is incorrect when it comes to dismissing modal view controllers you should use delegation. So before presenting your modal view make yourself the delegate and then call the delegate from the modal view controller to dismiss.


2 Answers

You can try this code for Swift:

let popup : PopupVC = self.storyboard?.instantiateViewControllerWithIdentifier("PopupVC") as! PopupVC let navigationController = UINavigationController(rootViewController: popup) navigationController.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext self.presentViewController(navigationController, animated: true, completion: nil) 

For swift 4 latest syntax using extension:

extension UIViewController {     func presentOnRoot(`with` viewController : UIViewController){         let navigationController = UINavigationController(rootViewController: viewController)         navigationController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext         self.present(navigationController, animated: false, completion: nil)     } } 

How to use:

let popup : PopupVC = self.storyboard?.instantiateViewControllerWithIdentifier("PopupVC") as! PopupVC self.presentOnRoot(with: popup) 
like image 31
Hardik Thakkar Avatar answered Nov 08 '22 01:11

Hardik Thakkar


First, remove all explicit setting of modal presentation style in code and do the following:

  1. In the storyboard set the ModalViewController's modalPresentation style to Over Current context

img1

  1. Check the checkboxes in the Root/Presenting ViewController - Provide Context and Define Context. They seem to be working even unchecked.
like image 92
Riskov Avatar answered Nov 08 '22 02:11

Riskov