iOS UIViewController跳转

iOS UIViewController跳转,第1张

1. UINavigationController跳转

UIViewController可以利用UINavigationController跳转,采用压栈和出栈的方式,进行UIViewController的管理。

// 添加指定控制器,并显示
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

// d出控制器,显示上一个控制器
- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated;
// d出控制器,显示指定控制器,指定控制器之前的所有控制器都将被d出
- (nullable NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
// d出控制器,显示根控制器,根控制器之前的所有控制器都将被d出
- (nullable NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated;
2. UIViewContoller模态跳转

UIViewContoller模态跳转可以d出指定控制器,跳转效果由modalPresentationStylemodalTransitionStyle决定。

// 跳转控制器的展示方式
@property(nonatomic,assign) UIModalPresentationStyle modalPresentationStyle;
// 跳转动画效果
@property(nonatomic,assign) UIModalTransitionStyle modalTransitionStyle;

这里有两个基础概念,一个是presenting view controllerpresented view controller,另一个是presentation context

presenting view controller和presented view controller

当VCA模态的d出了VCB,那么VCA就是presenting view controller,VCB就是presented view controller

[VCA presentViewController:VCB animated:YES completion:nil];

presentation context

当我们需要present VC的时候,除非我们指定了context,否则会优先选择presenting VC所属的容器类做为presentation context,如果没有容器类,那么会选择rootViewController。但是,搜索context的方式还与presented VC的modalPresentationStyle属性有关。

UIModalPresentationStyle跳转控制器的展示方式

类型描述
UIModalPresentationFullScreen默认的presentation style。 使用这种模式时,presented VC的宽高与屏幕相同,并且直接使用rootViewController做为presentation context,presentation context及其子VC都移出UI栈
UIModalPresentationPageSheet在常规型设备(大屏手机)的水平方向,presented VC的高为当前屏幕的高度,宽为该设备竖直方向屏幕的宽度,其余部分用透明背景做填充。对于紧凑型设备(小屏手机)的水平方向及所有设备的竖直方向,其显示效果与UIModalPresentationFullScreen相同
UIModalPresentationFormSheet在常规型设备的水平方向,presented VC的宽高均小于屏幕尺寸,其余部分用透明背景填充。对于紧凑型设备的水平方向及所有设备的竖直方向,其显示效果与UIModalPresentationFullScreen相同
UIModalPresentationCurrentContext宽高取决于presentation context的宽高,并且会寻找属性definesPresentationContext为YES的VC作为presentation context,presentation context及其子VC都将被暂时移出当前的UI栈。
UIModalPresentationCustom自定义模式,需要实现UIViewControllerTransitioningDelegate的相关方法,并将presented VC的transitioningDelegate 设置为实现了UIViewControllerTransitioningDelegate协议的对象。
UIModalPresentationOverFullScreen类似于UIModalPresentationFullScreen,唯一区别在于,presentation完成之后不会讲rootViewController移出当前的UI栈。
UIModalPresentationOverCurrentContext类似于UIModalPresentationCurrentContext,所不同的是presentation完成之后,不会将context及其子VC移出当前UI栈。但是,这种方式只适用于transition style为UIModalTransitionStyleCoverVertical的情况。
UIModalPresentationBlurOverFullScreenpresentation完成之后,如果presented VC的背景有透明部分,会看到presented VC下面的VC会变得模糊,其他与UIModalPresentationOverFullScreen模式没有区别。
UIModalPresentationPopover模态显示d出窗

UIModalTransitionStyle跳转动画效果

类型描述
UIModalTransitionStyleCoverVertical默认方式,从下往上
UIModalTransitionStyleFlipHorizontal水平翻转
UIModalTransitionStyleCrossDissolve淡入淡出
UIModalTransitionStylePartialCurl翻页效果

对话框效果

DialogViewController *dialogVc = [[DialogViewController alloc] init];
dialogVc.modalPresentationStyle = UIModalPresentationOverFullScreen;
dialogVc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

[self presentViewController:dialogVc animated:YES completion:^{
    NSLog(@"dialog show");
}];

d层效果

PopupViewController *popupVc = [[PopupViewController alloc] init];
popupVc.modalPresentationStyle = UIModalPresentationOverFullScreen;
popupVc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

[self presentViewController:popupVc animated:YES completion:^{
    NSLog(@"popup show");
}];

- (void)onShowPopoverClick:(UIButton *)sender {
    PresentModalPopoverViewController *vc = [[PresentModalPopoverViewController alloc] init];
    vc.preferredContentSize = CGSizeMake(200, 100);
    vc.modalPresentationStyle = UIModalPresentationPopover;
    
    UIPopoverPresentationController *popoverVc = [vc popoverPresentationController];
    // d出窗控制器代理
    popoverVc.delegate = self;
    // d出窗箭头方向
    popoverVc.permittedArrowDirections = UIPopoverArrowDirectionUp;
    // d出窗显示的视图资源
    popoverVc.sourceView = sender;
    // d出窗显示的区域
    popoverVc.sourceRect = sender.bounds;
    // d出窗背景颜色
    popoverVc.backgroundColor = [UIColor blueColor];
    
    [self presentViewController:vc animated:TRUE completion:^{
        NSLog(@"popover show");
    }];
}

// 需要实现代理方法,才能显示d窗
#pragma mark - UIPopoverPresentationControllerDelegate -
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone;
}

参考资料: https://blog.csdn.net/tianweitao/article/details/80314598

源码下载: https://github.com/nai-chen/IosBlog

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/996545.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存