前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >左右presentViewController经background黑问题

左右presentViewController经background黑问题

作者头像
全栈程序员站长
发布2022-07-06 17:07:44
4460
发布2022-07-06 17:07:44
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是全栈君

看效果图:

左右presentViewController经background黑问题
左右presentViewController经background黑问题

用例如以下代码,想弹出一个模态窗体,设置它的背景透明度为0.5,却发觉prsent后的背景色变为黑色的。

代码语言:javascript
复制
ShareVC *share = [[ShareVC alloc] init];
[self presentViewController:share animated:YES completion:nil];

起初还以为是设置透明度或者是[UIColor clearColor]出的问题,鼓捣几次之后发现不是这个问题。

google之后,在stackOverflow上找到几个比較靠谱的答案~

Why Does presentModalViewController:animated: Turn The Background Black?

Display clearColor UIViewController over UIViewController

终于结论为:

NavigationController and the View Controllers are designed in such a way that only one view controller may show at a time. When a new view controller is pushed/presented the previous view controller will be hidden by the system. So when you reduce the modal view’s alpha you will possibly see the window’s backgroundColor (the black color you see now).

If you want a translucent view to slide-in over the main view, you can add the view as the subView of main view and animate it using UIView Animations.

所以还是改用为 UIView Animations实现:

代码语言:javascript
复制
if (!_isShareViewOpen) {
 
_isShareViewOpen = YES;
 
ShareVC *shareVC = [[ShareVC alloc] initWithNibName:@"ShareVC" bundle:nil];
 
shareVC.shareVC = shareVC;
 
shareVC.awardList = self;
 
[self.view addSubview:shareVC.view];
 
[UIView animateWithDuration:0.75f animations:^{
 
shareVC.view.frame = CGRectMake(0, 640, self.view.frame.size.width, self.view.frame.size.height);
 
shareVC.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
 
} completion:^(BOOL finished) {
 
}];
 
}
左右presentViewController经background黑问题
左右presentViewController经background黑问题

————————————————————————–2014.09.26 再次编辑 ——————————————————————

假设你不想用动画来实现 present 的效果,能够设置 presentViewController 的属性,一样能够达到以上效果:

代码语言:javascript
复制
presentingViewController.modalPresentationStyle = <span style="font-family: Arial, Helvetica, sans-serif;">UIModalPresentationCurrentContext</span>;

————————————————————————–2015.01.22 再次编辑 ——————————————————————

以上代码仅仅是在 IOS7 中起作用。到 IOS8 就没用了,IOS8 能够用下面代码 fix:

代码语言:javascript
复制
_imagePickerCtrl.modalPresentationStyle = UIModalPresentationOverCurrentContext;
_imagePickerCtrl.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        
[appViews.rootViewController presentViewController:_imagePickerCtrl animated:YES completion:nil];
左右presentViewController经background黑问题
左右presentViewController经background黑问题

參考链接:

版权声明:本文博主原创文章,博客,未经同意不得转载。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Why Does presentModalViewController:animated: Turn The Background Black?
  • Display clearColor UIViewController over UIViewController
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档