ios 如何使用presentModalViewController创建透明视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/587681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use presentModalViewController to create a transparent view
提问by Darryl Braaten
I am displaying a modal view with
我正在显示一个模态视图
[self presentModalViewController:controller animated:YES];
When the view moves up the screen it is transparent as per the setting in the xib file it is created from, but once it fills the screen it goes opaque.
当视图在屏幕上向上移动时,根据创建它的 xib 文件中的设置,它是透明的,但是一旦它填满屏幕,它就会变得不透明。
Is there anyway of keeping the view transparent?
无论如何保持视图透明?
I suspect that the view it is being placed over is not being rendered rather then that the modal view is becoming opaque.
我怀疑它被放置的视图没有被渲染,而不是模态视图变得不透明。
采纳答案by Ben Gottlieb
Your view is still transparent, but once your modal controller is at the top of the stack, the view behind it is hidden (as is the case with any top-most view controller). The solution is to manually animate a view yourself; then the behind-viewController won't be hidden (since you won't have 'left' it).
您的视图仍然是透明的,但是一旦您的模态控制器位于堆栈顶部,它后面的视图就会隐藏(就像任何最顶部的视图控制器的情况一样)。解决方案是自己手动为视图设置动画;那么后面的viewController 将不会被隐藏(因为你不会“离开”它)。
回答by Infinite Possibilities
After iOS 3.2 there is a method to do this without any “tricks” – see the documentation for the modalPresentationStyle property. You have a rootViewController which will present the viewController. So here's the code to success:
在 iOS 3.2 之后,有一种方法可以在没有任何“技巧”的情况下执行此操作 -请参阅 modalPresentationStyle 属性的文档。您有一个 rootViewController,它将呈现 viewController。所以这是成功的代码:
viewController.view.backgroundColor = [UIColor clearColor];
rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[rootViewController presentModalViewController:viewController animated:YES];
With this method the viewController's background will be transparent and the underlying rootViewController will be visible. Please note that this only seems to work on the iPad,see comments below.
使用此方法,viewController 的背景将是透明的,而底层的 rootViewController 将是可见的。请注意,这似乎只适用于 iPad,请参阅下面的评论。
回答by midnightdavid
What I needed to get this to work:
我需要什么才能让它发挥作用:
self.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
回答by Kristopher Johnson
For those who want to see some code, here's what I added to my transparent view's controller:
对于那些想要查看一些代码的人,这是我添加到我的透明视图控制器中的内容:
// Add this view to superview, and slide it in from the bottom
- (void)presentWithSuperview:(UIView *)superview {
// Set initial location at bottom of superview
CGRect frame = self.view.frame;
frame.origin = CGPointMake(0.0, superview.bounds.size.height);
self.view.frame = frame;
[superview addSubview:self.view];
// Animate to new location
[UIView beginAnimations:@"presentWithSuperview" context:nil];
frame.origin = CGPointZero;
self.view.frame = frame;
[UIView commitAnimations];
}
// Method called when removeFromSuperviewWithAnimation's animation completes
- (void)animationDidStop:(NSString *)animationID
finished:(NSNumber *)finished
context:(void *)context {
if ([animationID isEqualToString:@"removeFromSuperviewWithAnimation"]) {
[self.view removeFromSuperview];
}
}
// Slide this view to bottom of superview, then remove from superview
- (void)removeFromSuperviewWithAnimation {
[UIView beginAnimations:@"removeFromSuperviewWithAnimation" context:nil];
// Set delegate and selector to remove from superview when animation completes
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
// Move this view to bottom of superview
CGRect frame = self.view.frame;
frame.origin = CGPointMake(0.0, self.view.superview.bounds.size.height);
self.view.frame = frame;
[UIView commitAnimations];
}
回答by Jeff C.
The Apple-approved way to do this in iOS 8 is to set the modalPresentationStyle to 'UIModalPresentationOverCurrentContext'.
Apple 认可的在 iOS 8 中执行此操作的方法是将 modalPresentationStyle 设置为“UIModalPresentationOverCurrentContext”。
From the UIViewController documentation:
从 UIViewController 文档:
UIModalPresentationOverCurrentContext
A presentation style where the content is displayed over only the parent view controller's content. The views beneath the presented content are not removed from the view hierarchy when the presentation finishes. So if the presented view controller does not fill the screen with opaque content, the underlying content shows through.
When presenting a view controller in a popover, this presentation style is supported only if the transition style is UIModalTransitionStyleCoverVertical. Attempting to use a different transition style triggers an exception. However, you may use other transition styles (except the partial curl transition) if the parent view controller is not in a popover.
Available in iOS 8.0 and later.
UIModalPresentationOverCurrentContext
一种显示样式,其中内容仅显示在父视图控制器的内容上。演示完成时,不会从视图层次结构中删除所呈现内容下方的视图。因此,如果呈现的视图控制器没有用不透明的内容填充屏幕,则底层内容会显示出来。
在弹出窗口中呈现视图控制器时,仅当过渡样式为 UIModalTransitionStyleCoverVertical 时才支持此呈现样式。尝试使用不同的过渡样式会触发异常。但是,如果父视图控制器不在弹出窗口中,您可以使用其他过渡样式(部分卷曲过渡除外)。
在 iOS 8.0 及更高版本中可用。
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/
The 'View Controller Advancements in iOS 8' video from WWDC 2014 goes into this in some detail.
来自 WWDC 2014 的“iOS 8 中的视图控制器改进”视频详细介绍了这一点。
Be sure to give your presented view controller a clear background color (otherwise, it will still appear opaque).
一定要给你呈现的视图控制器一个清晰的背景颜色(否则,它仍然会显得不透明)。
回答by Krumelur
There is another option: before showing the modal controller, capture a screenshot of the whole window. Insert the captured image into an UIImageView and add the image view to the controller's view you're about to show. Then send to back. Insert another view above the image view (background black, alpha 0.7). Show your modal controller and it looks like it was transparent. Just tried it on iPhone 4 running iOS 4.3.1. Like charm.
还有另一种选择:在显示模态控制器之前,捕获整个窗口的屏幕截图。将捕获的图像插入 UIImageView 并将图像视图添加到您将要显示的控制器视图中。然后送回去。在图像视图上方插入另一个视图(背景黑色,alpha 0.7)。显示您的模态控制器,它看起来是透明的。刚刚在运行 iOS 4.3.1 的 iPhone 4 上试了一下。喜欢魅力。
回答by lucaslt89
this is quite old, but i solved the same problem as follows: Since i need to present a navigation controller in iPhone, adding a subview wasn't a viable solution.
这已经很老了,但我解决了以下相同的问题:由于我需要在 iPhone 中显示导航控制器,因此添加子视图不是一个可行的解决方案。
So what i did:
所以我做了什么:
1) Before presenting the view controller, take a screenshot of your current screen:
1) 在展示视图控制器之前,先截取当前屏幕的截图:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
2) Create the view controller you want to present, and add the background as a subview, sending it to back.
2)创建你想要呈现的视图控制器,并将背景添加为子视图,将其发送到后面。
UIViewController * presentingVC = [UIViewController new];
UIImageView * backgroundImageOfPreviousScreen = [[UIImageView alloc] initWithImage:backgroundImage];
[presentingVC.view addSubview:backgroundImageOfPreviousScreen];
[presentingVC.view sendSubviewToBack:backgroundImageOfPreviousScreen];
3) Present your view controller, but before that in the new view controller, add a transparent view in the viewDidLoad (i used ILTranslucentView)
3)展示你的视图控制器,但在新的视图控制器之前,在viewDidLoad中添加一个透明视图(我使用了ILTranslucentView)
-(void)viewDidLoad
{
[super viewDidLoad];
ILTranslucentView * translucentView = [[ILTranslucentView alloc] initWithFrame:self.view.frame];
[self.view addSubview:translucentView];
[self.view sendSubviewToBack:translucentView];
}
And that's all!
就这样!
回答by Constantine
This appears to be broken in IOS 8, I am using a navigation controller and the context that is being displayed is the Navigation menus context which in our case is a sliding Menu controller.
这在 IOS 8 中似乎被破坏了,我使用的是导航控制器,并且正在显示的上下文是导航菜单上下文,在我们的例子中是滑动菜单控制器。
We are using pod 'TWTSideMenuViewController', '0.3' have not checked to see if this is an issue with the library yet or the method described above.
我们正在使用 pod 'TWTSideMenuViewController', '0.3' 尚未检查这是否是库或上述方法的问题。
回答by mkai
This worked to me in iOS 8-9:
这在 iOS 8-9 中对我有用:
1- Set your view controller's background with an alpha
1- 使用 alpha 设置视图控制器的背景
2- add this code:
2-添加此代码:
TranslucentViewController *tvc = [[TranslucentViewController alloc] init];
self.providesPresentationContextTransitionStyle = YES;
self.definesPresentationContext = YES;
[tvc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[self.navigationController presentViewController:tvc animated:YES completion:nil];
回答by Mr. T
I wrote down my findings about this in a different question, but the gist of it is that you have to call modalPresentationStyle = UIModalPresentationCurrentContext
on whatever owns the full screen at the moment. Most of the time, it's whatever is the [UIApplication sharedApplication].delegate.window's rootViewController. It could also be a new UIViewController that was presented with modalPresentationStyle = UIModalPresentationFullScreen
.
我在另一个问题中写下了我对此的发现,但其要点是您必须调用当前modalPresentationStyle = UIModalPresentationCurrentContext
拥有全屏的任何内容。大多数时候,它是 [UIApplication sharedApplication].delegate.window 的 rootViewController。它也可以是一个新的 UIViewController,带有modalPresentationStyle = UIModalPresentationFullScreen
.
Please read my other much more detailed post if you're wondering how I specifically solved this problem. Good luck!
如果您想知道我是如何具体解决这个问题的,请阅读我的其他更详细的帖子。祝你好运!