【问题标题】:Display clear colored ViewController over another ViewController in iOS 7 [duplicate]在 iOS 7 中的另一个 ViewController 上显示清晰的彩色 ViewController [重复]
【发布时间】:2013-10-29 11:21:51
【问题描述】:

在 iOS 7 之前,根据这个流行的 Stackoverflow question,,显示具有清晰背景的 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 中主要使用,因此透明视图控制器很可能是可能的。我还没有找到解决这个问题的方法,想知道是否有人知道如何解决这个问题。谢谢!

【问题讨论】:

  • modalPresentationStyle 文档现在指出:“在 iPhone 和 iPod touch 上,模态视图控制器总是全屏呈现,但在 iPad 上有几种不同的呈现选项”。所以我认为 Apple 在 iOS 7 中为 iPhone 取消了这个功能。
  • 这个stackoverflow.com/q/27598846/1603234让我微笑,现在轮到你了:)

标签: iphone ios ios7 transparency


【解决方案1】:

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

self.view.backgroundColor = [UIColor clearColor];

【讨论】:

    【解决方案2】:

    我只是按照原始解决方案在 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 快照:

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

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

    【讨论】:

    • 有一个类似的问题,我检查了你的代码并且工作正常;)谢谢@daspianist 的问题!
    • 如果主视图控制器嵌入在导航控制器中,则不起作用。
    • 我的背景是黑色的,你知道吗?? @codedad
    • 仅适用于 ipad 而不是 iphone :P
    • 不工作,显示黑色背景。
    【解决方案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];
    

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

    【讨论】:

    • 感谢您的解决方案 - 但这也不起作用。我在调用 VC 时仍然得到全黑背景。
    • 获得黑色背景
    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 2012-05-10
    • 2015-07-26
    • 2014-08-12
    • 1970-01-01
    • 2023-03-05
    相关资源
    最近更新 更多