问题描述
我有一个 UIViewController
视图作为另一个 UIViewController
视图之上的子视图/模式,例如子视图/模式应该是透明的,并且添加了任何组件子视图应该是可见的.问题是我的子视图显示黑色背景而不是 clearColor.我正在尝试将 UIView
设为 clearColor 而不是黑色背景.有人知道它有什么问题吗?任何建议表示赞赏.
I have a UIViewController
view as a subview/modal on top of another UIViewController
view, such as that the subview/modal should be transparent and whatever components is added to the subview should be visible. The problem is that I have is the subview shows black background instead to have clearColor. I'm trying to make UIView
as a clearColor not black background. Does anybody know what is wrong with it? Any suggestion appreciated.
FirstViewController.m
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:NO];
SecondViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.opaque = YES;
self.view.backgroundColor = [UIColor clearColor];
}
已解决:我解决了这些问题.它适用于 iPhone 和 iPad.没有黑色背景的模态视图控制器只是 clearColor/transparent.我唯一需要改变的是我将 UIModalPresentationFullScreen
替换为 UIModalPresentationCurrentContext
.多么简单!
RESOLVED: I fixed the issues. It is working so well for both of iPhone and iPad. Modal View Controller with no black background just clearColor/transparent. The only thing that I need to change is I replaced UIModalPresentationFullScreen
to UIModalPresentationCurrentContext
. How simple is that!
FirstViewController.m
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
注意:如果您使用 navigationController
的 modalPresentationStyle
属性:
NOTICE: If you are using a modalPresentationStyle
property of navigationController
:
FirstViewController.m
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
注意:坏消息是上述解决方案不适用于 iOS 7.好消息是我修复了 iOS7 的问题!我向某人求助,他是这样说的:
当以模态方式呈现视图控制器时,iOS 会在其呈现的持续时间内从视图层次结构中移除其下方的视图控制器.虽然以模态呈现的视图控制器的视图是透明的,但它下面除了应用程序窗口之外没有任何东西,它是黑色的.iOS 7 引入了一种新的模式呈现样式,UIModalPresentationCustom
,它导致 iOS 不会删除呈现的视图控制器下的视图.但是,为了使用这种模态呈现样式,您必须提供自己的过渡委托来处理呈现和关闭动画.这在 WWDC 2013 的使用视图控制器的自定义转换"演讲中进行了概述 https://developer.apple.com/wwdc/videos/?id=218,其中还介绍了如何实现您自己的过渡委托.
When presenting a view controller modally, iOS removes the view controllers underneath it from the view hierarchy for the duration it is presented. While the view of your modally presented view controller is transparent, there is nothing underneath it except the app window, which is black. iOS 7 introduced a new modal presentation style, UIModalPresentationCustom
, that causes iOS not to remove the views underneath the presented view controller. However, in order to use this modal presentation style, you must provide your own transition delegate to handle the presentation and dismiss animations. This is outlined in the 'Custom Transitions Using View Controllers' talk from WWDC 2013 https://developer.apple.com/wwdc/videos/?id=218 which also covers how to implement your own transition delegate.
您可能会在 iOS7 中看到我针对上述问题的解决方案:https://github.com/hightech/iOS-7-Custom-ModalViewController-Transitions
You may see my solution for the above issue in iOS7: https://github.com/hightech/iOS-7-Custom-ModalViewController-Transitions
推荐答案
RESOLVED:我解决了这些问题.它适用于 iPhone 和 iPad.没有黑色背景的模态视图控制器只是 clearColor/transparent.我唯一需要改变的是我将 UIModalPresentationFullScreen 替换为 UIModalPresentationCurrentContext.那是多么简单!
RESOLVED: I fixed the issues. It is working so well for both of iPhone and iPad. Modal View Controller with no black background just clearColor/transparent. The only thing that I need to change is I replaced UIModalPresentationFullScreen to UIModalPresentationCurrentContext. How simple is that!
FirstViewController.m
FirstViewController.m
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
注意:如果您使用了 navigationController 的 modalPresentationStyle 属性:
NOTICE: If you are using a modalPresentationStyle property of navigationController:
FirstViewController.m
FirstViewController.m
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
注意:坏消息是上述解决方案不适用于 iOS 7.好消息是我修复了 iOS7 的问题!我向某人求助,他是这样说的:
当以模态方式呈现视图控制器时,iOS 会在其呈现的持续时间内从视图层次结构中移除其下方的视图控制器.虽然以模态呈现的视图控制器的视图是透明的,但它下面除了应用程序窗口之外没有任何东西,它是黑色的.iOS 7 引入了一种新的模态呈现样式 UIModalPresentationCustom,它使 iOS 不会删除呈现的视图控制器下的视图.但是,为了使用这种模态呈现样式,您必须提供自己的过渡委托来处理呈现和关闭动画.这在 WWDC 2013 的使用视图控制器的自定义转换"演讲中进行了概述 https://developer.apple.com/wwdc/videos/?id=218,其中还介绍了如何实现您自己的过渡委托.
When presenting a view controller modally, iOS removes the view controllers underneath it from the view hierarchy for the duration it is presented. While the view of your modally presented view controller is transparent, there is nothing underneath it except the app window, which is black. iOS 7 introduced a new modal presentation style, UIModalPresentationCustom, that causes iOS not to remove the views underneath the presented view controller. However, in order to use this modal presentation style, you must provide your own transition delegate to handle the presentation and dismiss animations. This is outlined in the 'Custom Transitions Using View Controllers' talk from WWDC 2013 https://developer.apple.com/wwdc/videos/?id=218 which also covers how to implement your own transition delegate.
您可能会在 iOS7 中看到我针对上述问题的解决方案:https://github.com/hightech/iOS-7-Custom-ModalViewController-Transitions
You may see my solution for the above issue in iOS7: https://github.com/hightech/iOS-7-Custom-ModalViewController-Transitions
这篇关于在 UIViewController 上显示 clearColor UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!