presentViewController 方法中实现半透明背景效果

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

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

- (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];

}



  • definesPresentationContext
/*
  Determines which parent view controller's view should be presented over for presentations of type
  UIModalPresentationCurrentContext.  If no ancestor view controller has this flag set, then the presenter
  will be the root view controller.
*/
  • backgroundColor
设置你的背景颜色
  • modalPresentationStyle
/*
  Defines the transition style that will be used for this view controller when it is presented modally. Set
  this property on the view controller to be presented, not the presenter.  Defaults to
  UIModalTransitionStyleCoverVertical.
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值