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

15

在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];

然而,最近我在使用iOS 7时发现(并且其他人也在主回答中评论过),上述解决方案不再适用,而只是显示黑色的模态控制器。我知道透明度在iOS 7中被广泛使用,因此透明的视图控制器很可能是可行的。我还没有找到解决此问题的方法,并想知道是否有人知道如何解决这个问题。谢谢!


1
modalPresentationStyle 的文档现在说明:“在 iPhone 和 iPod touch 上,模态视图控制器始终以全屏方式呈现,但在 iPad 上有几种不同的呈现选项”。因此我认为 Apple 在 iOS 7 中将其从 iPhone 上移除了。 - Ortwin Gentz
1
这个 https://dev59.com/Sobca4cB1Zd3GeqPT0kK 让我微笑了,现在轮到你了 :) - Hemang
Swift版本:https://dev59.com/pWgu5IYBdhLWcg3wkHyP#34578402 - Mojtaba Yeganeh
3个回答

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];
}
  • 请查看IB快照: enter image description here

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

enter image description here

希望我没有误解您的问题 ;) 祝编码愉快!


1
遇到了类似的问题,我检查了你的代码并且可以运行 ;) 感谢 @daspianist 的提问! - Zsolt Normann
4
如果主视图控制器嵌入到导航控制器中,则无法正常工作。 - vocaro
8
我得到了黑色背景,你知道为什么吗?@codedad - S P Balu Kommuri
仅在 iPad 上工作,不支持 iPhone :P - Anil Kothari
无法工作,它显示黑色背景。 - Ajumal
这个在iOS 9上已经不再起作用了。 - Sebastian Hojas

-2

对于iOS 7,子类化SecondViewController并尝试在SecondViewController的viewDidLoad中设置视图背景颜色

self.view.backgroundColor = [UIColor clearColor];

-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];

尝试在视图加载后更改视图的背景颜色。 在您的解决方案中,您首先更改了视图并进行了呈现。视图生命周期不是以这种方式进行的。

4
谢谢你的解决方案,然而这个也没有起作用。在调用视图控制器时我仍然得到了全黑背景。 - daspianist
获取黑色背景 - Girijesh Kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接