Created
April 23, 2018 18:23
-
-
Save Edudjr/b19b3d07942c0015b02ef40a715c44cf to your computer and use it in GitHub Desktop.
Swift 4 Modal with transparent background
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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