#坑1# iOS8以后presendViewController透明效果无效

在iOS8及更高版本中,使用`modalPresentationStyle = .overCurrentContext`来设置弹出视图控制器的透明效果。以往的方法在这些版本上会导致背景显示为黑色。详细解决方案见原文链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文出自

http://mokai.github.io/2015/10/ios-bug-viewcontroller_translucent/

iOS弹出的视图背景默认是黑色,如果想去掉,以往我们使用 self.modalPresentationStyle = .CurrentContext 就可以实现效果,如以下半透明的代码

    let vc = UIViewController()
    vc.view.backgroundColor = UIColor(red:0.000 , green:0.000 , blue:0.000, alpha:0.5)
    self.modalPresentationStyle = .CurrentContext
    self.presentViewController(vc, animated: true, completion: nil)

但是你会发现这段代码在iOS8、iOS9上面运行依旧一片黑,因此iOS8以后得这么干

let vc = UIViewController()
vc.view.backgroundColor = UIColor(red:0.000 , green:0.000 , blue:0.000, alpha:0.5)
if let version = Float(UIDevice.currentDevice().systemVersion) where version >= 8{
    vc.modalPresentationStyle = .OverCurrentContext //注意此处是弹出VC对象,不是self
}else{
    self.modalPresentationStyle = .CurrentContext
}
self.presentViewController(vc, animated: true, completion: nil)

iOS8后增加了OverCurrentContext取代CurrentContext,并且设置的对象是弹出的VC

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值