美文网首页
如何实现一个半透明的ViewController

如何实现一个半透明的ViewController

作者: Iris_lin | 来源:发表于2016-08-14 01:46 被阅读0次

世界上最可怕的敌人不是老板,是产品!

前脚定了整个页面呈现的效果,写完了说改成卡片式,后面半透明到上一个页面。保持着对我乔爸爸的崇敬之心,坚信ViewController的本质都是view,就必须肯定能实现!

当然完全可以用view+动画的形式来呈现,只是在不想再改的情况下,偷了个懒。

要完成的效果(有改了效果和高斯模糊,但是写这个主要是为了说明半透明的ViewController,就随意看看):

首先,第一个想法是,直接把第二个页面设置黑背景透明度就好了!

    [self.view setBackgroundColor:RGBA(0, 0, 0, 0.3)];

然而事与愿违,得到的效果却是:


当页面加载完成的时候,背景会自动被一层黑色盖住,为什么会这样呢?!!

因为乔爸爸在NavigationController和 ViewController的默认设计中,keyWindow上一次只能有一个ViewController,而当一个新的ViewController被pushed/prensented出来的时候,前一个视图将会被隐藏,而所见的这层颜色,是KeyWindow的背景颜色。

官方API给ViewController提供了这两个属性
modalTransitionStyle 和 modalPresentationStyle

官方对它们作出的解释是:
它定义可转场的风格,设置这个属性,将改变prensent效果的模态,而这个属性,是在1页面到2页面跳转时候设置的,不是prensent的主导者,默认是UIModalTransitionStyleCoverVertical,这次要实现的效果,主要是modalPresentationStyle这个属性。

那么,在跳转时候,将这两个属性设置好,就能干掉那个狗屁的背景颜色,天知道我为了这两行代码研究了两小时。。。

    TwoViewController *view = [[TwoViewController alloc]init];

    view.modalPresentationStyle = UIModalPresentationOverFullScreen;
    view.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        
    [self presentViewController:view animated:YES completion:^{
    }];
typedef NS_ENUM(NSInteger, UIModalTransitionStyle) {
    UIModalTransitionStyleCoverVertical = 0, //默认的,垂直从下往上
    UIModalTransitionStyleFlipHorizontal,  //垂直翻转
    UIModalTransitionStyleCrossDissolve,//淡入淡出
    UIModalTransitionStylePartialCurl,//卷曲
};
typedef NS_ENUM(NSInteger, UIModalPresentationStyle) {
        UIModalPresentationFullScreen = 0, //默认
        UIModalPresentationPageSheet ,
        UIModalPresentationFormSheet ,
        UIModalPresentationCurrentContext,
        UIModalPresentationCustom ,
        UIModalPresentationOverFullScreen,
        UIModalPresentationOverCurrentContext,
        UIModalPresentationPopover,
        UIModalPresentationNone = -1,         
};

modalPresentationStyle对于这个属性,反正我每个都试了一遍,
因为字面意思真的让我觉得不理解,并且我一个设置了并没看出效果有什么不同。求科普!!!!!😨
CurrentContext是导航条保留,UIModalPresentationCustom、UIModalPresentationOverFullScreen和UIModalPresentationPopover满足当前需求,UIModalPresentationNone送了一个大崩。

相关文章

网友评论

      本文标题:如何实现一个半透明的ViewController

      本文链接:https://www.haomeiwen.com/subject/uurysttx.html