Skip to content

Instantly share code, notes, and snippets.

@Edudjr
Created April 23, 2018 18:23
Show Gist options
  • Save Edudjr/b19b3d07942c0015b02ef40a715c44cf to your computer and use it in GitHub Desktop.
Save Edudjr/b19b3d07942c0015b02ef40a715c44cf to your computer and use it in GitHub Desktop.
Swift 4 Modal with transparent background
//
// Created by Eduardo Domene Junior on 19/04/2018.
// Make sure that your view controller in Attributes Inspector is marked as:
// * Presentation - Over the current context
// * Defines context
// * Provides context
import UIKit
class ModalTransparentBackground: UIViewController {
var backgroundView = UIView()
override func viewDidLoad() {
view.backgroundColor = UIColor.clear
view.isOpaque = false
backgroundView.frame = view.bounds
backgroundView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
// Get ViewController presenting this modal
if let parent = self.presentingViewController {
backgroundView.frame = parent.view.bounds
backgroundView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
parent.view.addSubview(backgroundView)
}
}
override func viewWillDisappear(_ animated: Bool) {
backgroundView.removeFromSuperview()
}
@IBAction func okButtonTapped(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
deinit {
print("ModalTransparentBackground deinit")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment