LoginSignup
3
9

More than 5 years have passed since last update.

Swift3 - PresentsViewControllerで左右に画面遷移する方法

Last updated at Posted at 2017-05-07

概要

-Swift3
-Xcode8

やること

Swift3で左右に画面遷移しようとした時、pushviewcontroller使うのって若干、手間なので、PresentsViewControllerで左右に画面遷移しようと思いました。

pushviewcontrollerを使うよりも安易に動くし、設定できることも多いので、こっちを使うほうがいいと思っています。

別個のストーリーボード間の繊維

例えばMainストーリーボードからSettingという名前のストーリーボードに右遷移したい時は下記のように実装します。


            //ストーリーボードをインスタンス化
            let storyboard: UIStoryboard = UIStoryboard(name: "Setting", bundle: nil)
            let nextView = storyboard.instantiateInitialViewController()

           //右に遷移する
            let transition = CATransition()
            transition.duration = 0.5
            transition.type = kCATransitionPush

           //kCATransitionFromLeftにすれば左に遷移します
            transition.subtype = kCATransitionFromRight
            view.window!.layer.add(transition, forKey: kCATransition)
            present(nextView!, animated: false, completion: nil)

同じストーリーボード間での遷移

Mainストーリーボードから同じストーリーボードに作られたビューコンへの遷移は下記のようにします。withIdentifierを"Storyboard IDから下記のように設定してください。画像ではApplicationInformationというビューコンに飛ぼうとしてます

スクリーンショット 2017-06-06 12.13.22.png


let storyboard: UIStoryboard = self.storyboard!
            let nextView = storyboard.instantiateViewController(withIdentifier: "ApplicationInformation")
            let transition = CATransition()
            transition.duration = 0.5
            transition.type = kCATransitionPush
            transition.subtype = kCATransitionFromRight
            view.window!.layer.add(transition, forKey: kCATransition)
            present(nextView, animated: false, completion: nil)


参考

How to present view controller from right to left in iOS using Swift

Swift3 – PresentsViewControllerで左右に画面遷移する方法

3
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
9