Present ViewController Segue Transition Without Storyboard [Swift]

时间:2015-05-24 21:07:49

标签: ios iphone 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)

1 个答案:

答案 0 :(得分:22)

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)