presentViewController黑色背景而不是透明的

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

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

iOS 8 UIModalPresentationCurrentContext不透明?

这个解决scheme在Objective-C中引用了巴尔博亚的回答:

在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实现了这个function后,效果非常好。

只是在发现和尝试Ray Wenderlich论坛解决scheme之前,我看到了三个不同的SO问题 – 最近被标记为重复的问题。

据我所知,当你展示一个模型视图控制器时,不支持透明背景。 尝试在你的根视图控制器中保留控制器,并简单地将子视图添加到根视图控制器。

我有一个类似的问题,在创build控制器时出现短暂的黑色背景

  Disclaimer *vc = [[Disclaimer alloc]init]; 

我解决这个问题的方法是在IB中创build一个对应的对象,并使用它的storyboard ID实例化viewcontroller:

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

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

最后,它看起来不可能是透明的,我已经通过将这个视图添加到根视图控制器边界之外的子视图来解决这个问题,然后使用animation块将其滑动到位。 没有太多的额外的代码,但它能够使用标准的iOS行为来做到这一点很不错。