iOS – 半透明模式视图控制器

我想在当前的视图上以模态方式呈现一个略带透明背景的视图控制器,这样第一个视图在模态视图下稍微可见。

我设置了模式视图控制器的alpha值,并将modalPresentationStyle设置为UIModalPresentationCurrentContext ,如另一篇文章中的build议。

结果是animation时视图背景是透明的,但当视图控制器就位时,它变成不透明的黑色。 在解雇这个animation的过程中,它变得透明起来。

我怎样才能让它在活动时是透明的?

我已经在iOS 6 and 7testing。 我使用的代码如下:

 MyModalViewController *viewController = [[MyModalViewController alloc] init]; UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; [navController setNavigationBarHidden:YES]; self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext; [self.navigationController presentViewController:navController animated:YES completion:NULL]; 

iOS 8专门为此添加了一种新的模式演示风格:

 presentedViewController.modalPresentationStyle = UIModalPresentationOverFullScreen 

从规格 :

UIModalPresentationOverFullScreen

呈现视图覆盖屏幕的视图呈现样式。 演示完成后,所呈现内容下的视图不会从视图层次结构中删除。 所以如果呈现的视图控制器没有用不透明的内容填充屏幕,则底层内容显示通过。

如果您的目标是ios 8及以上版本,则可以将模式演示文稿样式设置为“over current context”,然后完成。 如果ios 7及以下,你将不得不创build一个自定义的过渡样式,以便在转换后呈现屏幕不会变空白。 这很复杂。

我提供的解决scheme提供了很大的灵活性:在显示模式对话框之前进行截图,并将其设置为应用程序窗口的背景图像。 默认情况下,该背景是黑色的(这是当后视控制器消失时所看到的)。 将背景更改为应用程序的屏幕截图。 在透明视图的viewWillAppear或viewDidLoad方法中制作屏幕截图。 这甚至与推段,不仅模式对话,但你应该避免animation。 一般情况下,避免影响背景视图位置的animation,因为这些animation看起来就像是在转换完成时会回到原来的位置。 在viewDidDissapear上将背景重置为之前的黑色图像以避免不必要的效果是个不错的主意。

你可以维护一堆这样的背景图片,你可以做多个“透明”推动seques。 或者有一些复杂/深层的菜单出现在主屏幕的顶部。 由于这些原因,我认为这个解决scheme比自己的转换代码更好。 这是更灵活,更容易实现,你不必自己处理animation。

显示模式之后,BG视图控制器消失的原因是iOS 7中的默认转换会在animation完成后删除BG视图。 如果你定义了你自己的转换,并且设置你的BG视图不被删除(只是改变它的alpha),那么你将拥有透明的模态视图。

这是一个解决scheme。

创build您的呈现视图控制器。 添加一个backView到这个视图控制器的主视图。 将其命名为backView

SecondViewController.m

 -(void)viewDidLoad { // Make the main view's background clear, the second view's background transparent. self.view.backgroundColor = [UIColor clearColor]; UIView* backView = [[UIView alloc] initWithFrame:self.view.frame]; backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6]; [self.view addSubview:backView]; } 

现在你有一个半透明背景的视图控制器。 你可以添加任何你想要的self.view ,其余的将是半透明的。

之后,在FirstViewController.m

 self.modalPresentationStyle = UIModalPresentationCurrentContext; [self presentViewController:secondViewController animated:YES completion:nil]; 

我的解决办法是:

创build自定义透明覆盖UIView,通过任何视图,导航栏和tabbbar。

– 在您的视图控制器embedded的导航控制器(或标签栏控制器)中创build一个自定义视图,其框架等于导航控制器视图的框架。

– 然后我把它的origin.y设置为navigationController.view.height

– 然后我创build了2个函数 – (void)showOverlay和 – (void)hideOverlay,在屏幕上和屏幕之外对覆盖视图进行animation处理:

 - (void)hideOverlay{ [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.3]; CGRect frm = self.helpView.frame;//helpView is my overlay frm.origin.y = self.offscreenOffset; //this is an Y offscreen usually self.view.height self.helpView.frame = frm; [UIView commitAnimations]; } - (void)showOverlay{ [self.view bringSubviewToFront:self.helpView]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.3]; CGRect frm = self.helpView.frame; frm.origin.y = self.onscreenOffset; self.helpView.frame = frm; [UIView commitAnimations]; } 

– 在我的视图控制器,我可以打电话

 [(MyCustomNavCtrl *)self.navigationController showOverlay]; [(MyCustomNavCtrl *)self.navigationController hideOverlay]; 

这就是它。

FYI:现在的语法是:

  childVC.modalPresentationStyle = UIModalPresentationStyle.OverFullScreen 

你为什么不尝试在AppDelegate中设置

 self.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext; 

然后改变正在呈现的视图上的阿尔法