Display and dismiss a modal view controller in Swift

不问归期 提交于 2020-06-24 07:11:10

问题


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:


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)



回答2:


To Dismiss View Controller in Swift 3.0

self.dismiss(animated: true, completion: {})



回答3:


You can use presentViewController:animated:completion: and dismissViewControllerAnimated:completion: methods from UIViewController. See docs here




回答4:


Its pretty easy :

to dismiss a modal view with swift 3.0 : Use dismiss Api like below :

> @IBAction func dismissClick(_ sender: Any) {
>         dismiss(animated: true, completion: nil)
>         
>     }

For present :

> @IBAction func dismissClick(_ sender: Any) {
> present(UIViewController(), animated: true, completion: nil)
>         
>     }

For more details here you go :

https://developer.apple.com/documentation/uikit/uiviewcontroller#//apple_ref/doc/uid/TP40006926-CH3-SW96




回答5:


Dismiss view controller in Swift 4:

dismiss(animated: true, completion: nil)


来源:https://stackoverflow.com/questions/28139547/display-and-dismiss-a-modal-view-controller-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!