美文网首页#iOS#HeminWoniOS开发iOS开发
模态(modal)一个部分透明的ViewController

模态(modal)一个部分透明的ViewController

作者: Lucifer_Lin | 来源:发表于2016-02-22 21:41 被阅读1229次

前言

最近项目遇到一个需求:从一个viewController A模态出一个新的viewController B, 要求B占领屏幕的下半部分,上半部分透明.如下图:

要达到的效果

思路是将B控制器的背景颜色设置为透明色(注意,不是设置alpha = 0, 而是self.view.backgroundColor = [UIColor clearColor];),但是发现这样的话,屏幕的上半部分会变黑,如下图:

坑爹的效果

本文记录了实现该效果的方法.

干货

经过google, 最终发现是因为modal时,需要设置弹出的控制器(B)的属性.

- (void)modal {
    UIViewController *B = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"ViewControllerTow"];
    B.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    B.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:B animated:YES completion:nil];
}

另外

有一篇文章中写道, 以上设置是ios8之后才能用到的代码,ios8之前是设置A控制器的,即谁弹出控制器,谁就设置以下属性:

- (void)modal {
    UIViewController *two = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"ViewControllerTow"];
    self.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self presentViewController:two animated:YES completion:nil];
}

综上

- (void)modal {
    UIViewController *two = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"ViewControllerTow"];
    float version = [UIDevice currentDevice].systemVersion.floatValue;
    if (version < 8.0) {
        self.modalPresentationStyle = UIModalPresentationCurrentContext;
    } else {
    [self presentViewController:two animated:YES completion:nil];
    }
}

由于手头没有8.0以下版本的设备,所以希望有条件的童鞋帮忙测试一下,并反馈给我,谢谢大家.

相关文章

网友评论

  • liwb:作者你好 为什么我设置了UIModalPresentationCurrentContext的属性 不起作用 反而是设置了UIModalPresentationOverFullScreen 的属性 prsent出的VC是透明的?作者遇到这样问题了吗
    代码 如下
    BViewController *bVC = [[BViewController alloc] init];
    bVC.view.backgroundColor = [UIColor clearColor];
    bVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [self presentViewController:bVC animated:YES completion:NULL];
  • key3board:帮我填坑了! nice

本文标题:模态(modal)一个部分透明的ViewController

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