Dar*_*ten 80 iphone uiviewcontroller ios
我正在显示模态视图
[self presentModalViewController:controller animated:YES];
Run Code Online (Sandbox Code Playgroud)
当视图向上移动屏幕时,它根据创建的xib文件中的设置是透明的,但是一旦它填满屏幕就会变得不透明.
有没有保持视图透明?
我怀疑它被放置的视图没有被渲染,而是模态视图变得不透明.
Inf*_*ies 85
在iOS 3.2之后,有一种方法可以在没有任何"技巧"的情况下执行此操作 - 请参阅modalPresentationStyle属性的文档.你有一个rootViewController,它将呈现viewController.所以这是成功的代码:
viewController.view.backgroundColor = [UIColor clearColor];
rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[rootViewController presentModalViewController:viewController animated:YES];
Run Code Online (Sandbox Code Playgroud)
使用此方法,viewController的背景将是透明的,底层的rootViewController将是可见的.请注意,这似乎只适用于iPad,请参阅下面的评论.
Ben*_*ieb 62
您的视图仍然是透明的,但是一旦您的模态控制器位于堆栈的顶部,它背后的视图就会被隐藏(就像任何最顶层的视图控制器一样).解决方案是自己手动设置视图动画; 那么后面的viewController将不会被隐藏(因为你不会'离开'它).
小智 48
我需要让它工作:
self.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
Run Code Online (Sandbox Code Playgroud)
Kri*_*son 24
对于那些想看一些代码的人来说,这就是我添加到透明视图控制器中的内容:
// 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];
}
Run Code Online (Sandbox Code Playgroud)
Jef*_* C. 19
在iOS 8中Apple批准的方法是将modalPresentationStyle设置为'UIModalPresentationOverCurrentContext'.
从UIViewController文档:
UIModalPresentationOverCurrentContext
一种演示样式,其中内容仅显示在父视图控制器的内容上.演示文稿完成后,不会从视图层次结构中删除所显示内容下方的视图.因此,如果呈现的视图控制器没有用不透明的内容填充屏幕,则底层内容会显示出来.
在弹出框中显示视图控制器时,仅当过渡样式为UIModalTransitionStyleCoverVertical时才支持此演示样式.尝试使用不同的过渡样式会触发异常.但是,如果父视图控制器不在弹出框中,则可以使用其他过渡样式(部分卷曲过渡除外).
适用于iOS 8.0及更高版本.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/
来自WWDC 2014的"在iOS 8中查看控制器进展"视频详细介绍了这一点.
请务必为您呈现的视图控制器提供清晰的背景颜色(否则,它仍将显示为不透明).
Kru*_*lur 16
还有另一种选择:在显示模态控制器之前,捕获整个窗口的屏幕截图.将捕获的图像插入UIImageView,并将图像视图添加到您即将显示的控制器视图中.然后发送回来.在图像视图上方插入另一个视图(背景黑色,alpha 0.7).显示你的模态控制器,看起来它是透明的.刚刚在运行iOS 4.3.1的iPhone 4上试过.喜欢魅力.
luc*_*t89 10
这是相当古老的,但我解决了同样的问题如下:由于我需要在iPhone中提供导航控制器,添加子视图不是一个可行的解决方案.
所以我做了什么:
1)在呈现视图控制器之前,请截取当前屏幕的屏幕截图:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
2)创建要显示的视图控制器,并将背景添加为子视图,然后将其发送回去.
UIViewController * presentingVC = [UIViewController new];
UIImageView * backgroundImageOfPreviousScreen = [[UIImageView alloc] initWithImage:backgroundImage];
[presentingVC.view addSubview:backgroundImageOfPreviousScreen];
[presentingVC.view sendSubviewToBack:backgroundImageOfPreviousScreen];
Run Code Online (Sandbox Code Playgroud)
3)展示您的视图控制器,但在此之前在新视图控制器中,在viewDidLoad中添加透明视图(我使用了ILTranslucentView)
-(void)viewDidLoad
{
[super viewDidLoad];
ILTranslucentView * translucentView = [[ILTranslucentView alloc] initWithFrame:self.view.frame];
[self.view addSubview:translucentView];
[self.view sendSubviewToBack:translucentView];
}
Run Code Online (Sandbox Code Playgroud)
就这样!
我在另一个问题中写下了关于此问题的发现,但是要点是,您现在必须调用modalPresentationStyle = UIModalPresentationCurrentContext拥有全屏显示的所有内容。大多数时候,它是[UIApplication sharedApplication] .delegate.window的rootViewController。也可能是与一起提供的新UIViewController modalPresentationStyle = UIModalPresentationFullScreen。
如果您想知道我是如何专门解决此问题的,请阅读我的其他更详细的文章。祝好运!
小智 5
这似乎在IOS 8中被打破,我正在使用导航控制器,正在显示的上下文是导航菜单上下文,在我们的示例中是滑动菜单控制器.
我们正在使用pod'TWTSideMenuViewController','0.3'尚未检查这是否是库的问题或上述方法.
小智 5
这在iOS 8-9中对我有用:
1-使用alpha设置视图控制器的背景
2-添加此代码:
TranslucentViewController *tvc = [[TranslucentViewController alloc] init];
self.providesPresentationContextTransitionStyle = YES;
self.definesPresentationContext = YES;
[tvc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[self.navigationController presentViewController:tvc animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
89483 次 |
| 最近记录: |