在iOS 7中的另一个ViewController上显示清晰的彩色ViewController

das*_*ist 15 iphone transparency ios ios7

在iOS 7之前,根据这个流行的Stackoverflow 问题,显示具有清晰背景的ViewController的方法是在主ViewController中执行以下操作:

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

但是,正如我最近在iOS 7中发现的那样(以及其他人对主要答案的评论),上述解决方案不再有效,而只是显示了黑色模型控制器.我知道透明度主要用于iOS 7,因此透明视图控制器很有可能.我还没有发现这个问题的解决方法,并且想知道是否有人知道如何解决这个问题.谢谢!

nzs*_*nzs 8

我只是按照原始解决方案并在Interface Builder中应用了一些默认和自定义设置,在我看来它正在工作.

重要部分(与问题代码非常相似):

- (IBAction)click:(id)sender {
    NSLog(@"click");


    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"TopOverVc"];

    vc.view.backgroundColor = [UIColor clearColor];
    self.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self presentViewController:vc animated:NO completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
  • 也请找IB快照: 在此输入图像描述

  • 和模拟器结果(按下按钮后):

在此输入图像描述

希望我没有误解你的问题;)快乐的编码!

  • 我得到了黑色背景你知道吗?@codedad (8认同)
  • 如果主视图控制器嵌入在导航控制器中,则不起作用. (4认同)

Sat*_*zad -3

为什么你不使用这个

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];

    self.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self presentViewController:vc animated:NO completion:nil];

 vc.view.backgroundColor = [UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)

加载视图后尝试更改视图的背景颜色。在你的第一次改变视图和呈现中。视图生命周期不会以这种方式进行

  • 感谢您提供的解决方案 - 但这也不起作用.. 在致电 VC 时我仍然得到全黑背景。 (4认同)