Swift Unable to present view controller from top to bottom

Mahesh Narla picture Mahesh Narla · Jan 3, 2017 · Viewed 10.5k times · Source

In my application I have to present the screen from Top to bottom and I have tried below code its giving the same normal presenting style.

        let screen = self.storyboard?.instantiateViewController(withIdentifier: "Screen1p5") as? Screen1p5
        let transition = CATransition()
        transition.duration = 0.5
        transition.type = kCATransitionPush
        transition.subtype = kCATransitionFromTop
        view.window!.layer.add(transition, forKey: kCATransition)
        self.present(screen!, animated: true, completion: nil)

Answer

Nirav D picture Nirav D · Jan 3, 2017

For that you need to set subtype of CATransition to kCATransitionFromBottom and for batter animation set animated to false with present(_:animated:completion:).

let screen = self.storyboard?.instantiateViewController(withIdentifier: "Screen1p5") as? Screen1p5
let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromBottom
view.window!.layer.add(transition, forKey: kCATransition)
self.present(screen!, animated: false, completion: nil)

For dismiss set subtype of CATransition to kCATransitionFromTop.

let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromTop
view.window!.layer.add(transition, forKey: kCATransition)
self.dismiss(animated: false)