Swift Unable to present view controller from top to bottom

12.5k views Asked by At

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)
2

There are 2 answers

7
Nirav D On BEST ANSWER

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)
9
Ramkrishna Sharma On

Just changed it transition.subtype to kCATransitionFromBottom

transition.subtype = kCATransitionFromBottom

For dismiss the controller.

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

Please find the below GIF representation.

GIF

If you are using the .XIB then please find the below code.

For present the controller.

let newController = NewViewController(nibName: "NewView", bundle: nil)
let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromBottom
view.window!.layer.add(transition, forKey: kCATransition)
self.present(newController, animated: true, completion: nil)

For dismiss the controller. It is the same code as above.

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