半透明模态视图

 今天开发遇到一个问题,就是想在原来视图控制器上弹出一个半透明模态视图;

第一次尝试一般这样写

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


以下是我英语四级不到水平翻译结果:

ios对内存的使用是非常“抠门”的,

所以当模态视图覆盖了原来视图后,

在把原来视图加载就没多大意义了,

所以,ios卸载了原来视图呈现了模态视图

你可以通过-viewWillDisappear: and -viewDidDisappear:这两个方法来检测这种行为


解决方法呢  可以参考so上这个

http://stackoverflow.com/questions/587681/how-to-use-presentmodalviewcontroller-to-create-a-transparent-view/5579145#5579145



也就是说自己定义一个半透明视图控制器,自定义动画让他从底部弹出来,这个方法楼主本来采用了,但是后来出了点问题,没有再深究,换了个方法



通过这个属性设置,但是注意最后一句话“Please note that this only seems to work on the iPad” ipad有木有坑爹呢!!

查看了别的文章有说

typedef enum {
    UIModalPresentationFullScreen = 0,
    UIModalPresentationPageSheet,
    UIModalPresentationFormSheet,
    UIModalPresentationCurrentContext,
} UIModalPresentationStyle;
UIModalPresentationFullScreen代表弹出VC时,presented VC充满全屏,如果弹出VC的wantsFullScreenLayout设置为YES的,则会填充到状态栏下边,否则不会填充到状态栏之下。

  UIModalPresentationPageSheet代表弹出是弹出VC时,presented VC的高度和当前屏幕高度相同,宽度和竖屏模式下屏幕宽度相同,剩余未覆盖区域将会变暗并阻止用户点击,这种弹出模式下,竖屏时跟UIModalPresentationFullScreen的效果一样,横屏时候两边则会留下变暗的区域。

  UIModalPresentationFormSheet这种模式下,presented VC的高度和宽度均会小于屏幕尺寸,presented VC居中显示,四周留下变暗区域。

  UIModalPresentationCurrentContext这种模式下,presented VC的弹出方式和presenting VC的父VC的方式相同。

  这四种方式在iPad上面统统有效,但在iPhone和iPod touch上面系统始终已UIModalPresentationFullScreen模式显示presented VC。


---------好吧原谅楼主罗嗦了半天,最后我是这样解决的,还是通过  UIModalPresentationCurrentContext 属性,不过是需要通过当前控制器拿到window对象的rootViewControl设置,这个应该是因为模态视图都是从跟控制器上弹出的有关(不确定),

self.view.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;

以上这句为本文核心,self为当前控制器对象,over


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值