Back Button Animation in Navigation Controller

后端 未结 5 1814
执笔经年
执笔经年 2021-02-10 19:39

CATransitions can be used to animate transitions in Navigation Controllers when drilling down. However when using Back button og Navigation Controller (going back up) animation

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-10 19:48

    For adding animation to the back button, you have to create your own back button, and on the back button action specify the animation you want.

    1. Adding a back button to the navigation bar: add this line to your viewDidLoad method:

      self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(back)];
      
    2. In the back method add this code:

      CATransition *transition = [CATransition animation];
      transition.duration = 1;
      transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
      transition.type = kCATransitionPush;
      transition.subtype = kCATransitionFromBottom;
      transition.delegate = self;
      [self.navigationController.view.layer addAnimation:transition forKey:nil];
      [self.navigationController popViewControllerAnimated:YES];
      

提交回复
热议问题