如何从屏幕顶部而不是底部显示UIViewController?

问题描述:

我正在使用UIViewController,我使用presentModalViewController:controllerr animated:YES来呈现它,但我想如果它会从屏幕的顶部向下滑动而不是从底部向上滑动,有任何方法可以做到这一点?如何从屏幕顶部而不是底部显示UIViewController?

您所描述的内容不是内置过渡风格;有关Apple提供的列表,请参阅here。如果你绝对需要你的视图控制器以这种方式出现,你必须自己动画。

我创建了一个功能,从所有4个方向推ViewControllers

- (void) slideLayerInDirection:(NSString *)direction andPush:(UIViewController *)dstVC { 
    CATransition* transition = [CATransition animation]; 
    transition.duration = 0.5; 
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
    transition.type = kCATransitionPush; 
    transition.subtype = direction; 
    [self.view.layer addAnimation:transition forKey:kCATransition]; 
    [self pushViewController:dstVC animated:NO]; 
} 

头文件包含以下内容:

#import <QuartzCore/QuartzCore.h> 

#define direction_left kCATransitionFromLeft 
#define direction_top kCATransitionFromBottom 
#define direction_right kCATransitionFromRight 
#define direction_bottom kCATransitionFromTop 
+0

您正在使用push,但原来的问题是指模态演示风格。你知道这是否可以调整工作? –

+0

我在顶部到底部和底部到顶部的转换中使用相同的代码,看起来像Modal。 – Amarsh

+0

如果在转换时出现黑色闪烁,只需在app delegate(self.window.backgroundColor = [UIColor whiteColor];)中将窗口颜色更改为目标视图控制器的纯色背景色。 – Pawan

你必须#importQuartzCore框架,并添加过渡动画,然后更改:

animated:YESanimated:NO

我使用下面的代码来从顶部对viewController进行动画处理。

[self.mainViewController.view addSubview:modalViewController.view]; 
modalViewController.view.frame = CGRectMake(modalViewController.view.frame.origin.x, 
              -modalViewController.view.frame.size.height, 
              modalViewController.view.frame.size.width, 
              modalViewController.view.frame.size.height); 

[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void){ 

    modalViewController.view.frame = CGRectMake(modalViewController.view.frame.origin.x, 
               0, 
               modalViewController.view.frame.size.width, 
               modalViewController.view.frame.size.height); 

} completion:^(BOOL finished){ 

    [modalViewController.view removeFromSuperview]; 
    [self.mainViewController presentViewController:modalViewController animated:NO completion:nil]; 

}]; 

它添加modalViewControllerUIViewmainViewController,在动画它,然后删除该mainViewControllerpresentsViewController:UIView没有动画。

public extension UINavigationController { 

    func pushViewControllerFromTop(viewController vc: UIViewController) { 
     vc.view.alpha = 0 
     self.presentViewController(vc, animated: false) {() -> Void in 
      vc.view.frame = CGRectMake(0, -vc.view.frame.height, vc.view.frame.width, vc.view.frame.height) 
      vc.view.alpha = 1 
      UIView.animateWithDuration(1, animations: {() -> Void in 
       vc.view.frame = CGRectMake(0, 0, vc.view.frame.width, vc.view.frame.height) 
        }, 
        completion: nil) 
      } 
    } 

    func dismissViewControllerToTop() { 
      if let vc = self.presentedViewController { 
      UIView.animateWithDuration(1, animations: {() -> Void in 
       vc.view.frame = CGRectMake(0, -vc.view.frame.height, vc.view.frame.width, vc.view.frame.height) 
      }, completion: { (complete) -> Void in 
       if complete == true { 
        self.dismissViewControllerAnimated(false, completion: nil) 
       } 
      }) 
      } 
    } 
}