美文网首页
iOS 模态出一个半透明的ViewController pres

iOS 模态出一个半透明的ViewController pres

作者: 爱上火烧的小毛驴 | 来源:发表于2017-12-28 16:07 被阅读584次

最近项目有需求, 需要模态初一个半透明的视图, 好多人都碰到这个问题吧, 在目标视图中设置背景颜色然后发现模态动作结束后变成了黑色或者不是半透明的颜色。

所以今天来告诉大家解决方案

- (IBAction)Avtion1:(id)sender {

    TestViewController * testVC = [TestViewController new];

    self.definesPresentationContext = YES; //self is presenting view controller
    testVC.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.4];
    testVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;

    [self presentViewController:testVC animated:YES completion:nil];
}
注意:如果present 一个NavController,不能完全使用上面代码。
- (IBAction)pushSecond:(id)sender{

    SecondViewController * testVC = [SecondViewController new];

    

    self.definesPresentationContext = YES; //self is presenting view controller

    testVC.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.5];

//    testVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:testVC];

    nav.modalPresentationStyle = UIModalPresentationOverCurrentContext;

    nav.view.backgroundColor = [UIColor clearColor];

    [self presentViewController:nav animated:YES completion:nil];

}

相关文章

网友评论

      本文标题:iOS 模态出一个半透明的ViewController pres

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