2

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)
Nirav D
  • 71,513
  • 12
  • 161
  • 183
Mahesh Narla
  • 342
  • 2
  • 3
  • 20

2 Answers2

20

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)
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • Thanks for your answer i will try and let you know. – Mahesh Narla Jan 03 '17 at 10:20
  • It is worked fine but while dismissing view it should dismiss from bottom to top direction, not top to bottom again, Will you please tell me how to dismiss the presented controller from bottom to top. – Mahesh Narla Jan 03 '17 at 10:29
  • That was nice solution its working fine . . While presenting controller in log i'm getting message like "Presenting view controllers on detached view controllers is discouraged" will it effect to application. – Mahesh Narla Jan 03 '17 at 10:36
  • 1
    @MaheshNarla Check this link for that warning http://stackoverflow.com/questions/19890761/warning-presenting-view-controllers-on-detached-view-controllers-is-discourage – Nirav D Jan 03 '17 at 10:39
  • yes bro its helped me and i updated my code as follows then warning message disappeared self.view.window?.rootViewController?.present(screen!, animated: false, completion: nil) Thank you again – Mahesh Narla Jan 03 '17 at 11:02
  • @MaheshNarla Welcome mate :) – Nirav D Jan 03 '17 at 11:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132181/discussion-between-mahesh-narla-and-nirav-d). – Mahesh Narla Jan 03 '17 at 11:14
4

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)
Ramkrishna Sharma
  • 6,961
  • 3
  • 42
  • 51
  • @Ramakrishna sharma It is worked fine and while dismissing view it should dismiss from bottom to top direction, not top to bottom again, Will you please tell me how to dismiss the presented controller from bottom to top. – Mahesh Narla Jan 03 '17 at 10:30
  • Update the answer please review it. – Ramkrishna Sharma Jan 03 '17 at 10:37
  • Thanks @MaheshNarla and up-voting the answer. – Ramkrishna Sharma Jan 03 '17 at 10:44
  • Will you please tell me how to present xib to viewcontroller – Mahesh Narla Jan 06 '17 at 04:31
  • It means that you are using xib instead of Storyboard, right. – Ramkrishna Sharma Jan 06 '17 at 06:25
  • Yes as per requirement i have to use xib instead of storyboard for one view controller – Mahesh Narla Jan 06 '17 at 06:32
  • For this i have rised another separate question The issue was resolved I got the same answer from there, anyway thank you bro for your response – Mahesh Narla Jan 06 '17 at 07:34
  • why does it show a flash screen of black color? I am encountering the same issue but unable to resolve it. Can anybody help? I tried putting tint color to white in appDelegate file but it didn't help. – ADK May 17 '17 at 09:34
  • hey @RamkrishnaSharma I really hate when the view controller in the back turn to black in last moment of the transition , how can I fix this , I try to set the view controller modalPresentationStyle property to .overCurrentContext but it's not working – mazen Sep 21 '18 at 20:16