swift: 画面遷移の方法

swift4での画面遷移の方法を説明します。

遷移先のUIViewControllerのIdentity InspectorにてStoryboard IDを記載します。
f:id:englishskill:20180617001617p:plain

遷移元のUIViewControllerにてボタンを設定後、そのボタンのIBActionで、

@IBAction func nextpage(_ sender: Any) {
let storyboard: UIStoryboard = self.storyboard!
let second = storyboard.instantiateViewController(withIdentifier: "second")
self.present(second, animated: true, completion: nil)
}

これで、画面が遷移できます。

画面を右から左に遷移するには、

@IBAction func nextpage(_ sender: Any) {
let storyboard: UIStoryboard = self.storyboard!
let second = storyboard.instantiateViewController(withIdentifier: "second")
let transition = CATransition()
transition.duration = 1.0
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromLeft
view.window!.layer.add(transition, forKey: kCATransition)
self.present(second, animated: true, completion: nil)
}

戻るときには、

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

これで戻れます。