ViewController Where you need to open dialog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import UIKit class CusomDialogViewController: UIViewController, DialogDeligate { @IBOutlet weak var dialogValue: UILabel! // Init dialogVC fileprivate let dialogVC = DialogViewController() override func viewDidLoad() { super.viewDidLoad() //set delegate dialogVC.delegate = self dialogVC.dialogTitle = "Custom Title" //Add Dialog in current VC addDialogVC() } fileprivate func addDialogVC() { // 2- Add Dialog as a child view self.addChild(dialogVC) self.view.addSubview(dialogVC.view) dialogVC.didMove(toParent: self) // 3- Adjust Dialog frame and initial position. let height = view.frame.height let width = view.frame.width dialogVC.view.frame = CGRect(origin: CGPoint(x:0, y:self.view.frame.maxY), size: CGSize(width:width, height:height)) } @IBAction func openDialog(_ sender: Any) { dialogVC.open() } //Impliment delegate function which returns user entered string from dialog func submit(value: String?) { dialogValue.text = value } } |
Dialog ViewController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import UIKit //Create protocol for callback if you wants to pass //some data from dialog protocol DialogDeligate { func submit(value:String?) } class DialogViewController: UIViewController { @IBOutlet weak var _dialogTitle: UILabel! @IBOutlet weak var userValue: UITextField! var dialogTitle:String = "Dialog Title" var delegate:DialogDeligate? override func viewDidLoad() { super.viewDidLoad() _dialogTitle.text = dialogTitle } @IBAction func didTapClose(_ sender: Any) { close() } @IBAction func didTapSubmit(_ sender: Any) { delegate?.submit(value: userValue.text) close() } func open(){ UIView.animate(withDuration: 0.3) { [weak self] in let frame = self?.view.frame let yComponent = 0//UIScreen.main.bounds.height //- 300 self?.view.frame = CGRect(origin: CGPoint(x:0, y:yComponent), size: CGSize(width:frame!.width, height:frame!.height)) } } fileprivate func close() { UIView.animate(withDuration: 0.3) { [weak self] in let frame = self?.view.frame let yComponent = UIScreen.main.bounds.height //- 300 self?.view.frame = CGRect(origin: CGPoint(x:0, y:yComponent), size: CGSize(width:frame!.width, height:frame!.height)) } } } |