iosbegindevel
iosbegindevel

Reputation: 337

How can I show a modal screen in swift5?

I am currently working on the iOS project.

My problem is that when I see the new screen, I don't see the old screen and I only see the new one.

I want a modal screen. What am I missing?

move ModalScreen

    func callAlert(_ text: String!) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let myAlert = storyboard.instantiateViewController(withIdentifier: "ModalViewController") as! ModalViewController
        myAlert.modalPresentationStyle = .overCurrentContext
        myAlert.modalTransitionStyle = .crossDissolve
        myAlert.text = text
        self.present(myAlert, animated: false, completion: nil)
    }

modalScreenController

class ModalViewController : UIViewController {

    var text: String?


    @IBOutlet weak var modalCustomAlert: UITextView!
    @IBOutlet weak var okButton: UIButton!

    @IBAction func okPress(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        modalCustomAlert.layer.cornerRadius = 8
        self.modalCustomAlert.text = text
    }
...

modalStoryBoard

imagestoryboard

Upvotes: 1

Views: 300

Answers (1)

Torongo
Torongo

Reputation: 1091

You need to set a background color with alpha component in your viewcontroller's view.

class ModalViewController : UIViewController {

    var text: String?


    @IBOutlet weak var modalCustomAlert: UITextView!
    @IBOutlet weak var okButton: UIButton!

    @IBAction func okPress(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.black.withAlphaComponent(0.5) //Your desired color and alpha component
        modalCustomAlert.layer.cornerRadius = 8
        self.modalCustomAlert.text = text
    }

Upvotes: 1

Related Questions