A simple way to present your child view controller on top of another one. Read relevant article: https://andreygordeev.com/2017/04/18/overlay-view-controller-protocols-swift/.
Written on Swift.
Make the parent view controller conforming to OverlayHost
protocol and call showOverlay(_:)
method when needed:
class ViewController: UIViewController, OverlayHost {
@IBAction func showOverlayButtonPressed() {
showOverlay(type: MessageViewController.self, fromStoryboardWithName: "Main")
}
}
Make the parent view controller conforming to OverlayViewController
protocol and call dismissOverlay()
method when you want to dismiss the overlay:
class MessageViewController: UIViewController, OverlayViewController {
let overlaySize: CGSize? = CGSize(width: UIScreen.main.bounds.width * 0.8, height: 120.0)
@IBAction func closeButtonPressed() {
dismissOverlay()
}
}
On storyboard, set view controller's Storyboard ID to MessageViewController
.
Just drop OverlayViewController.swift
into your project and you're all set.
This project is under MIT license. For more information, see LICENSE
file.