在iOS上present一个半透明的viewController

今天尝试着在一个ViewController上面调用:

- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated

来展示一个半透明的viewController:

    UIViewController *vc = [[[UIViewController alloc] init] autorelease];
    vc.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [self presentModalViewController:vc animated:YES];

这样可以发现在动画过程中是半透明的,但是动画结束后就看不到下面一个viewController的内容了,变黑色了。

为什么呢?搜索了一番得到一份比较合理的结论:

The “problem” is that iOS is very finicky about not wasting memory,

and since the modal view will completely cover the one beneath it,

it doesn’t make much sense to keep it loaded.

Therefore, iOS unloads the view that presents the modal one.

You may check this behavior by implementing -viewWillDisappear: and -viewDidDisappear:.

最终在SO上找到这么个问题,以及一份可行的方案

viewController.view.backgroundColor = [UIColor clearColor];
rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[rootViewController presentModalViewController:viewController animated:YES];

这里有两个点:一是设置modalPresentationStyle为UIModalPresentationCurrentContext,二是需要在rootViewController上操作。

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值