iOS UIViewController跳转

1. UINavigationController跳转

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

主要方法

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

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

场景一,从A跳转到B

UIViewController *BVC = [[UIViewController alloc] init];
[self.navigationController pushViewController:BVC animated:true];

场景二,从B返回到A

[self.navigationController popViewControllerAnimated:YES];

场景三,从B返回到指定C

for (UIViewController *vc in self.navigationController.viewControllers) {
    if ([vc isKindOfClass:[CSampleViewController class]]) {
        [self.navigationController popToViewController:vc animated:YES];
        return;
    }
}

2. UIViewContoller模态跳转

UIViewContoller模态跳转可以弹出指定控制器,跳转效果由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模态的弹出了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 VCmodalPresentationStyle属性有关,可参考这个

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模态显示弹出窗

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");
}];

在这里插入图片描述

弹层效果

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];
    // 弹出窗控制器代理
    popoverVc.delegate = self;
    // 弹出窗箭头方向
    popoverVc.permittedArrowDirections = UIPopoverArrowDirectionUp;
    // 弹出窗显示的视图资源
    popoverVc.sourceView = sender;
    // 弹出窗显示的区域
    popoverVc.sourceRect = sender.bounds;
    // 弹出窗背景颜色
    popoverVc.backgroundColor = [UIColor blueColor];
    
    [self presentViewController:vc animated:TRUE completion:^{
        NSLog(@"popover show");
    }];
}

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

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值