iOS 透明视图控制器

来源:互联网 发布:全国行政区数据库 编辑:程序博客网 时间:2024/04/29 10:07

我们有时候需要使用弹出框,当然我们可以使用alertview,但是alertview的效果是有限的,

所以这个时候,我们就可以使用视图控制器了。很多情况下,我们都像让我们的视图控制器背景透明,

在网上查找N久之后,终于发现

ios8多了一个样式UIModalPresentationOverCurrentContext,跟原来的很相近哦。

而且定义的对象也变成了弹出的视图。所以用的话做版本判断:

if ([[[UIDevice currentDevicesystemVersionfloatValue]>=8.0) {

        nextVC.modalPresentationStyle=UIModalPresentationOverCurrentContext;

 }else{

self.modalPresentationStyle=UIModalPresentationCurrentContext;

  }

在我的程序中的使用场景如下,CalendarController是我自定义的一个视图控制器,


-(void) showCalender:(UITapGestureRecognizer*) gesture{

    CalendarController* transparentView = [[CalendarControlleralloc] init];

    if ([[[UIDevicecurrentDevice] systemVersion]floatValue]>=8.0) {

        transparentView.modalPresentationStyle=UIModalPresentationOverCurrentContext;

    }else{

        self.modalPresentationStyle=UIModalPresentationCurrentContext;        

    }

    

    

    transparentView.view.backgroundColor = [UIColorcolorWithRed:0green:0 blue:0 alpha:0.5];

    

   UIViewController* controller = [SettingControllerrootController];

    controller.modalPresentationStyle =UIModalPresentationCurrentContext;

    

    [controller presentViewController:transparentViewanimated:NOcompletion:^{///弹出控制器

        ///控制器出现后调用

    }];

}


弹出之后,我们就得让他消失,一般情况下,我们弹出控制器使用的是

[self.navigationControllerpopViewControllerAnimated:NO];

然而,者并没什么卵用,既然我们弹出控制器不是使用一般的方法,那么我们让其消失,也不应该用一般的方法,

不信,您用一般方法试一下。那么下面就是见证奇迹的时候了:

[selfdismissViewControllerAnimated:NOcompletion:^{//调用此方法即可

        //同样的,您可以在在这里做你想做的事

    }];


干货分享到此结束,欢迎给位客官享用,并不吝赐教!


1 0
原创粉丝点击