LoginSignup
5
5

More than 5 years have passed since last update.

UIViewの上に置いたUIButtonでPresentsViewControllerを呼び出す方法

Posted at

環境

・Swift3
・Xcode8

ぬえええええ!!!!!!ウイベウフォw

スクリーンショット 2017-05-15 13.18.46.png

上記のようにUIViewの上にUIButtonを置いてみればPresentsViewControllerを呼び出せず画面遷移ができません。困り果てました、最悪です。

infoClick()関数を定義する

UIButtonのActionの中でinfoClick()関数を呼び出してみてください。画面遷移ができます!


func infoClick()  {

        let storyboard: UIStoryboard = UIStoryboard (name: "Main", bundle: nil)
        let vc: CampainDetailView = storyboard.instantiateViewControllerWithIdentifier("campainDetailView") as! CampainDetailView
        let currentController = self.getCurrentViewController()
        currentController?.presentViewController(vc, animated: false, completion: nil)

    }

この関数は、ルートビューコントローラーをクリエイトするぜ!

func getCurrentViewController() -> UIViewController? {

    if let rootController = UIApplication.sharedApplication().keyWindow?.rootViewController {
        var currentController: UIViewController! = rootController
        while( currentController.presentedViewController != nil ) {
            currentController = currentController.presentedViewController
        }
        return currentController
    }
    return nil

}

左右に遷移する

アプリっぽく左右に遷移させたければ下記のように書けばいいと思います。全ソースを載せておきます。

 @IBAction func SettingButton(_ sender: Any) {

        let storyboard: UIStoryboard = UIStoryboard (name: "Time", bundle: nil)

        let vc: TimeViewController = storyboard.instantiateViewController(withIdentifier: "Time") as! TimeViewController
        let currentController = self.getCurrentViewController()

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

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


    }

    func getCurrentViewController() -> UIViewController? {

        if let rootController = UIApplication.shared.keyWindow?.rootViewController {
            var currentController: UIViewController! = rootController
            while( currentController.presentedViewController != nil ) {
                currentController = currentController.presentedViewController
            }
            return currentController
        }
        return nil

    }

参考

How can I call presentViewController in UIView class?

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

UIViewの上に置いたUIButtonでPresentsViewControllerを呼び出す方法

5
5
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
5
5