用黑色背景代替透明的背景呈现视图控制器

8

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

5个回答

22
这是可能的,rockybalboa 在 raywenderlich.com 的论坛帖子中提供了一个解决方案:iOS 8 UIModalPresentationCurrentContext is not transparent? 这个解决方案是引用balboa的回答,在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

使用Xcode 6.3 beta 3和Swift 1.2在iOS 8.1上实现后,它可以完美地工作。

只是想评论一下,在找到并尝试Ray Wenderlich论坛的解决方案之前,我查看了三个不同的SO问题 - 最近的一个现在被标记为重复


我不得不使用:modalPresentationStyle = .OverFullScreen。使用.OverCurrentContext会导致很多自动布局问题。 - Le Mot Juiced
我在模态视图控制器上得到了黑色背景,不知道为什么。 - apinho

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

0

使用SWIFT 4,只需将此代码添加到您想要具有透明背景的视图控制器中即可。

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

}

问题明确说明了他们的视图具有半透明背景。 - James Wolfe

0

我曾经遇到过类似的问题,当使用控制器创建时,黑色背景会在短暂延迟后出现。

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

我的问题得以解决的方法是在IB中创建一个相应的对象,然后使用该对象的故事板ID实例化视图控制器:
     Disclaimer *vc = (Disclaimer *)[self.storyboard instantiateViewControllerWithIdentifier:@"SBIDDisclaimer"];
[self presentViewController:vc animated:YES completion:nil];

我猜通过IB进行操作会进行一些额外的初始化。


0
最终看起来似乎无法实现透明效果,我通过将此视图作为子视图添加到根视图控制器的边界之外,并使用动画块将其滑入到位来解决了这个问题。虽然没有太多额外的代码,但如果能够使用标准的iOS行为来完成这个任务会更好。

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