iOS - 半透明模态视图控制器

Dre*_*w C 23 uiviewcontroller uinavigationcontroller modalviewcontroller ios

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

我设置模态视图控制器的alpha值并设置modalPresentationStyleUIModalPresentationCurrentContext,如另一篇文章中所建议的那样.

结果是视图背景在动画制作时是透明的,但是当视图控制器就位时,它会变为不透明的黑色.它可以恢复透明,同时激活解雇.

如何在活动时让它变得透明?

我已经测试过了iOS 6 and 7.我使用的代码如下:

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];
Run Code Online (Sandbox Code Playgroud)

jbu*_*s20 39

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

presentedViewController.modalPresentationStyle = UIModalPresentationOverFullScreen
Run Code Online (Sandbox Code Playgroud)

规格:

UIModalPresentationOverFullScreen

一种视图演示样式,其中呈现的视图覆盖屏幕.演示文稿完成后,不会从视图层次结构中删除所显示内容下方的视图.因此,如果呈现的视图控制器没有用不透明的内容填充屏幕,则底层内容会显示出来.

  • 这是正确的答案.现在我们所要做的就是等待ios 7死掉 (3认同)
  • 必须为呈现的"modalPresentationStyle"设置,而不是为呈现视图控制器设置.您可能希望相应地编辑答案中的变量. (2认同)

alp*_*r_k 1

这是一个解决方案。

创建您的呈现视图控制器。将 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];
}
Run Code Online (Sandbox Code Playgroud)

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

之后,在FirstViewController.m

self.modalPresentationStyle = UIModalPresentationCurrentContext;

[self presentViewController:secondViewController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)