Push Up View Controller from Bottom using Swift
Asked Answered
P

6

24

I would like to push a view controller using Swift and animate it so it appears from the bottom and moves up. I have the following code to push my view controller:

let helloTableViewController = self.storyboard!.instantiateViewControllerWithIdentifier("helloTableViewController") as! HelloTableViewController
self.navigationController!.pushViewController(helloTableViewController, animated: true)

I have found the following from another question but cannot seem to get it to work in Swift:

CATransition *animation = [CATransition animation]; 
[animation setDuration:2]; 
[animation setType:kCATransitionPush]; 
[animation setSubtype:kCATransitionFromTop]; 
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 
SecondView *sObj=[[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
[self.navigationController pushViewController:sObj animated:YES];
[[sObj.view layer] addAnimation:animation forKey:@"SwitchToView1"];
Pneumatics answered 12/8, 2015 at 16:22 Comment(2)
You need to show the translated swift code. Also, what do you mean by "cannot get it to work"? Aside from this, the Objective-C code is quite badly written (it is confusing of them to create a view controller called SecondView), and these days the above transition could more easily (should) be written using the UIView transitionWithView/transitionFromView method.Halden
Is there any reason why you can't just present the view controller modally? The modal transition comes up from the bottom.Missal
C
33

Here is the code for pushing

let transition:CATransition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name:CAMediaTimingFunctionName.easeInEaseOut)
transition.type = CATransitionType.push
transition.subtype = CATransitionSubtype.fromBottom
self.hostNavController?.view.layer.add(transition, forKey: kCATransition)
self.hostNavController?.popViewController(animated: true)

and type of transition you can use are

kCATransitionFromLeft
kCATransitionFromBottom
kCATransitionFromRight
kCATransitionFromTop
Clarhe answered 13/5, 2016 at 23:10 Comment(2)
How can we do similar animation using segues?Schaefer
I think using segue it is not possible. I think method swizzling can help you on thatClarhe
A
48

Swift3:

for push :

    // push view controller but animate modally
    let storyBoard: UIStoryboard = UIStoryboard(name: "myStoryBoard", bundle: nil)
    let vc = storyBoard.instantiateViewController(withIdentifier: "myViewControllerIdentifier") as! MyViewController

    let navigationController = self.navigationController


    vc.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Close", style: .plain, target: vc, action: #selector(vc.closeView))
    vc.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: vc, action: nil)

    let transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionMoveIn
    transition.subtype = kCATransitionFromTop
    navigationController?.view.layer.add(transition, forKey: nil)
    navigationController?.pushViewController(vc, animated: false)

and in vc for pop:

func closeView() {
    let transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionReveal
    transition.subtype = kCATransitionFromBottom
    navigationController?.view.layer.add(transition, forKey: nil)
    _ = navigationController?.popViewController(animated: false)
}
Anima answered 10/5, 2017 at 19:44 Comment(2)
This fades the alpha of the view being transitioned from, meaning I can see underneath it during the transition. Is there a way to prevent this behavior?Pheidippides
@Pheidippides To prevent that, use UIViewControllerAnimatedTransitioning: https://mcmap.net/q/115908/-how-to-change-the-push-and-pop-animations-in-a-navigation-based-appLamphere
C
33

Here is the code for pushing

let transition:CATransition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name:CAMediaTimingFunctionName.easeInEaseOut)
transition.type = CATransitionType.push
transition.subtype = CATransitionSubtype.fromBottom
self.hostNavController?.view.layer.add(transition, forKey: kCATransition)
self.hostNavController?.popViewController(animated: true)

and type of transition you can use are

kCATransitionFromLeft
kCATransitionFromBottom
kCATransitionFromRight
kCATransitionFromTop
Clarhe answered 13/5, 2016 at 23:10 Comment(2)
How can we do similar animation using segues?Schaefer
I think using segue it is not possible. I think method swizzling can help you on thatClarhe
A
10

Updated with Swift 5 and Xcode 11.4

Controller push in present animation style

    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = storyBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController

    let transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
    transition.type = CATransitionType.moveIn
    transition.subtype = CATransitionSubtype.fromTop
    self.navigationController?.view.layer.add(transition, forKey: nil)
    self.navigationController?.pushViewController(viewController, animated: false)

PopToView Controller in Dismiss Style

            let transition:CATransition = CATransition()
        transition.duration = 0.5
        transition.timingFunction = CAMediaTimingFunction(name:CAMediaTimingFunctionName.easeInEaseOut)
        transition.type = CATransitionType.reveal
        transition.subtype = CATransitionSubtype.fromBottom
        self.navigationController?.view.layer.add(transition, forKey: kCATransition)
        self.navigationController?.popViewController(animated: false)
Ambo answered 30/4, 2020 at 4:47 Comment(0)
P
5

I had a similar situation where I needed to show a navigation item with some UIBarButtonItems on a view controller, but when presenting modally, it wasn't showing up. It was showing only when pushing from a navigationController, but I really needed the animation from bottom -> up. I tried the CATransition examples from above, but it brought up another bug where there was like a black shadow / translucent background during the animation. I came up with a pretty simple solution in the end:

let someVC = storyboard?.instantiateViewController(withIdentifier: "SomeVC") as! SomeVC
let navController = UINavigationController(rootViewController: someVC)
navigationController?.present(navController, animated: true, completion: nil)

And to close the view controller and get rid of the navigation controller altogether, you just call:

self.dismiss(animated: true, completion: nil)

This will dismiss the view controller, and will release our navigationController from memory.

It is a simple solution in the end, but I spent some time until coming up with it. Maybe I'll save someone else's time :)

Phono answered 30/7, 2019 at 13:46 Comment(2)
Thank you, this did save my time! The animation I found above (and in other answers) moves the current view controller as it places another one; this one pushes a controller on top of the existing, which is what I was looking for.Lillie
I am glad this one helped someone already)) Good luck and happy coding!Phono
D
5

Swift 5.0 And Later

  let transition:CATransition = CATransition()
  transition.duration = 0.5
  transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
  transition.type = CATransitionType.push
  transition.subtype = CATransitionSubtype.fromTop
  self.navigationController!.view.layer.add(transition, forKey: kCATransition)
Dalpe answered 30/10, 2019 at 11:17 Comment(0)
P
3

Swift 4.2+ solution based on Nex Mishra's answer:

let transition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
transition.type = .push
transition.subtype = .fromBottom
navigationController?.view.layer.add(transition, forKey: kCATransition) 
navigationController?.pushViewController([destination VC], animated: false)
Procuration answered 15/6, 2019 at 21:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.