简体   繁体   中英

Present ViewController Segue Transition Without Storyboard [Swift]

I'm relatively new to Swift and I'm trying to present a new View Controller with a fade-in as opposed to the default modal animation (appear from bottom). I am not using storyboards and I wanted to see if there's a good way to do this programmatically. I tried using modalTransitionStyle but I think I may not have implemented it properly. Here is my code:

    var modalStyle: UIModalTransitionStyle = UIModalTransitionStyle.CrossDissolve
    StartViewController().modalTransitionStyle = modalStyle
    presentViewController(StartViewController(), animated: true, completion: nil)

Each time you call StartViewController() you are creating a new one. Instead, put that into a constant so that you can refer to the same one:

let modalStyle = UIModalTransitionStyle.CrossDissolve
let svc = StartViewController()
svc.modalTransitionStyle = modalStyle
presentViewController(svc, animated: true, completion: nil)

You can skip creating modalStyle and just set the modalTransitionStyle directly:

let svc = StartViewController()
svc.modalTransitionStyle = .CrossDissolve
presentViewController(svc, animated: true, completion: nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM