iOS VC modal样式的应用

1,038 阅读3分钟

这是我参与8月更文挑战的第9天,活动详情查看: 8月更文挑战” juejin.cn/post/698796… ”

前言

需求:将当前的VC的界面作为另一个VC的背景图片(在当前视图中推出另外一个背景透明的视图控制器)

具体的应用场景:

  1. 下单的的付款详情
  2. 发布商品的选择商品类目
  3. 反馈页面 在这里插入图片描述 下单的的付款详情这个界面属于独立的支付插件

实现思路:

  1. 使用系统自带的modal样式:UIModalPresentationOverCurrentContext - A presentation style where the content is displayed over another view controller’s content

  2. 截取当前屏幕

I 、实现方案

推荐使用系统自带的modal样式实现需求,因为性能更好,实现也简单

1.1 使用系统自带的modal样式

设置modalPresentationStyle为UIModalPresentationOverCurrentContext,并设置蒙版颜色

    CategoryViewController *tmp = [CategoryViewController new];
    tmp.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        //设置蒙版颜色
    [tmp view].backgroundColor = STModalWindowDefaultBackgroundColor;

   [self.navigationController presentViewController:tmp animated:YES completion:^{
        
        
    }];

    

子视图背景颜色可根据需要为clearColor,或者其他颜色。

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置VCView背景颜色为clearColor
    [self vcView].backgroundColor =[UIColor clearColor] ;

}

设置点击蒙版回到上个界面

设置tableView的点击事件优先级,低于cell的选中事件以及按钮的点击事件UIControlEventTouchUpInside


- (CRMSelectedWechantActivityTypeV    *)vcView{
    if (!_vcView) {
        
        
        CRMSelectedWechantActivityTypeV *tmp = [[CRMSelectedWechantActivityTypeV   alloc] initWithViewModel:self.viewModel];
        
        _vcView= tmp;
        __weak __typeof__(self) weakSelf = self;
        
        [self.view addSubview:tmp];
        [tmp mas_makeConstraints:^(MASConstraintMaker *make) {
            
            CGFloat CategoriesH =1*(142+12);
            make.height.mas_equalTo(kAdjustRatio(78+CategoriesH+90));
            make.left.equalTo(weakSelf.view).offset(kAdjustRatio(0));
            make.bottom.equalTo(weakSelf.view);
            make.right.equalTo(weakSelf.view).offset(-kAdjustRatio(0));
// 设置子视图约束(高度)
make.height.equalTo(weakSelf.view).multipliedBy(0.45).offset(kAdjustRatio(self.viewModel.selectedplatProductCategories.count*50+50));

        }];
        
        
        UITapGestureRecognizer *cutTap = [[UITapGestureRecognizer alloc] init];
        
        
        
        [[cutTap rac_gestureSignal] subscribeNext:^(id x) {
            
            [weakSelf dismissViewControllerAnimated:YES completion:nil];            
        }];
        
        [weakSelf.view addGestureRecognizer:cutTap];
        
        
        
                UITapGestureRecognizer *cutTap1 = [[UITapGestureRecognizer alloc] init];
                
                
                [[cutTap1 rac_gestureSignal] subscribeNext:^(id x) {
                    
                    NSLog(@" 设置tableView的点击事件优先级,低于cell的选中事件  ");

                    
                }];
                
                        cutTap1.cancelsTouchesInView = NO;// 设置tableView的点击事件优先级,低于cell的选中事件

                [tmp.tableView addGestureRecognizer:cutTap1];
        
    }
    return _vcView;
}

效果:发布商品的选择商品类目

在这里插入图片描述 下载地址:download.csdn.net/download/u0…

文章地址:kunnan.blog.csdn.net/article/det…

视频地址:live.csdn.net/v/167208

商品经营类目选择视图的应用场景: 1、发布商品时选择商品类目 2、商户进件选择经营类目 3、购物类app下单界面的商品类目筛选

你可以使用运行API进行全局控制modal的样式

从CSDN资源下载完整demo源码:download.csdn.net/download/u0… 灵活控制模态展示的视图样式的文章:blog.csdn.net/z929118967/…

更多内容请关注公众号:iOS逆向


- (NSMutableArray *)OverCurrentContextClasss{
    
    if(_OverCurrentContextClasss == nil){
        
        _OverCurrentContextClasss = [NSMutableArray array];
        
        
        
        //.. 发布商品- 选择商品类目
        
        [_OverCurrentContextClasss addObject:@"ERPSelect_commodity_categoryViewController"];

        // 图片浏览器
        
        [_OverCurrentContextClasss addObject:@"KNImageBrowserViewController"];
        // 选择微信活动
        [_OverCurrentContextClasss addObject:@"CRMSelectedWechantActivityTypeVC"];

        
        
    }
    return _OverCurrentContextClasss;
    
}

1.2 截取当前视图

反馈页面自动生成截图

注意:截图在弹反馈页面之前create 【原理文章】(https://kunnan.blog.csdn.net/article/details/113444297) 在这里插入图片描述 在这里插入图片描述

1.3 关于dismiss的使用总结

应用1:自定义WebViewController,使用完相册之后导致WebView 所在的控制器也被干掉的问题

  1. 问题:苹果的一个特性。当模态出N个ViewController之后,只需要dismiss任意一个,都会dismiss它之后的所有模态试图 ,因此会导致modal模态出来的UIViewController中WebView的H5弹出Camera/ImagePicker 时,当UIDocumentMenuViewController消失的时候会导致WebView 所在的控制器也被干掉。

  2. 解决思路 所以使dismissViewControllerAnimated调用一次,或者让UIDocumentMenuViewController找不到presentingViewController即可。

  3. 获取demo源码,请关注公众号:iOS逆向 github.com/zhangkn/KNU…

应用2:想要一次性把模态出来的所有ViewController dismiss,只需要使用presentingViewController进行dismiss即可

    [t setDissblock:^(id  _Nonnull sender) {
        [weakSelf.presentingViewController dismissViewControllerAnimated:YES completion:nil];
        
    }];

see also

git 代码分支管理教程

mp.weixin.qq.com/s/-63si1cRx…