【问题标题】:presentViewController black background instead of transparentpresentViewController 黑色背景而不是透明
【发布时间】:2012-10-29 05:46:51
【问题描述】:

我希望以标准方式(从屏幕底部向上滑动)呈现给用户的视图。这个视图的大约一半是透明背景,下半部分有一些输入字段(想象一下键盘弹出的方式)。当我在 rootViewController 上调用 [self presentViewController] 时,它会向上滑动视图,但大约半秒后,视图过去是透明的,它被替换为黑色。 presentViewController 和 presentModalViewController 都会发生这种情况。如何改变这种行为?

【问题讨论】:

    标签: ios ios5 ios6


    【解决方案1】:

    这是可能的,rockybalboa 在raywenderlich.com 的论坛帖子中提供了解决此问题的方法:

    iOS 8 UIModalPresentationCurrentContext is not transparent?

    该解决方案是在 Objective-C 中引用 balboa 的回答:

    在 iOS 8 之前,您可以这样做:

    [backgroundViewController setModalPresentationStyle:UIModalPresentationCurrentContext];
    [backgroundViewController presentViewController:_myMoreAppsViewController animated:NO completion:nil];
    

    在 iOS 8 中,您必须这样做:

    backgroundViewController.providesPresentationContextTransitionStyle = YES;
    backgroundController.definesPresentationContext = YES;
    [overlayViewController setModalPresentationStyle:UIModalPresentationOverCurrentContext];
    

    用等效的 Swift 补充上述代码:

    backgroundViewController.providesPresentationContextTransitionStyle = true
    backgroundController.definesPresentationContext = true
    overlayViewController.modalPresentationStyle = .OverCurrentContext
    

    在 iOS 8.1 和 Swift 1.2 上使用 Xcode 6.3 beta 3 实现了这一点,它运行良好。

    在查找和尝试 Ray Wenderlich 论坛解决方案之前,我查看了三个不同的 SO 问题 - 最近标记为重复 的评论。

    【讨论】:

    • 我必须使用:modalPresentationStyle = .OverFullScreen。使用.OverCurrentContext 会导致很多自动布局问题。
    • 我在那个模态视图控制器上得到黑色背景,不知道为什么
    【解决方案2】:

    据我所知,当您呈现模型视图控制器时,不支持透明背景。尝试将控制器保留在根视图控制器中,然后将子视图添加到根视图控制器。

    【讨论】:

      【解决方案3】:

      最后,它看起来不可能是透明的,我通过将此视图添加为根视图控制器边界之外的子视图来解决这个问题,然后使用动画块。没有太多额外的代码,但如果能够使用标准的 iOS 行为来完成它会很好。

      【讨论】:

        【解决方案4】:

        在使用

        创建控制器时,我遇到了类似的问题,即在短暂延迟后出现黑色背景
            Disclaimer *vc = [[Disclaimer alloc]init];
        

        对我来说解决问题的是在 IB 中创建一个相应的对象并使用它的故事板 ID 实例化视图控制器:

             Disclaimer *vc = (Disclaimer *)[self.storyboard instantiateViewControllerWithIdentifier:@"SBIDDisclaimer"];
        [self presentViewController:vc animated:YES completion:nil];
        

        我猜想通过 IB 来做一些额外的初始化。

        【讨论】:

          【解决方案5】:

          使用 SWIFT 4,只需将此代码添加到您希望拥有透明背景的视图控制器。

          override func viewDidLoad()
          {
              super.viewDidLoad()
              self.modalPresentationStyle = .overFullScreen
              self.view.backgroundColor = UIColor.clear
          
          }
          

          【讨论】:

          • 问题清楚地表明他们的视图具有半透明背景。
          猜你喜欢
          • 2011-02-06
          • 2013-09-21
          • 2015-04-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-05
          • 1970-01-01
          • 2012-09-10
          • 2016-02-22
          相关资源
          最近更新 更多