Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display and dismiss a modal view controller in Swift

Tags:

ios

swift

segue

When a button is pressed I want to segue between two view controllers by using a Modal Transition style CoverVertical and then dismiss it. There is allot of info out there for how to do it in objective C but can't find any good info in Swift. So far I've done this but I don't think it's correct:

 @IBAction func insertStatus(sender: UIButton) {

         var StatusVC: StatusViewController = StatusViewController()
    var modalStyle: UIModalTransitionStyle = UIModalTransitionStyle.CoverVertical
    StatusVC.modalTransitionStyle = modalStyle
    self.presentViewController(StatusVC, animated: true, completion: nil)

    }

The Dismiss I am using like so does not work either:

@IBAction func statusSaved(sender: UIBarButtonItem) {

        self.dismissViewControllerAnimated(false, completion: { () -> Void in
            let usersVC: UsersViewController = self.storyboard?.instantiateViewControllerWithIdentifier("UsersViewController") as UsersViewController
       })
    }

1 Answers

Swift 5:

present(UIViewController(), animated: true, completion: nil)

dismiss(animated: true, completion: nil)

Swift 2.2:

self.presentViewController(true, completion: nil)

Hide/dismiss a view controller:

self.dismissViewControllerAnimated(true, completion: nil)
like image 72
Cesare Avatar answered Sep 15 '25 04:09

Cesare