清除颜色使背景为黑色

5

我希望在我的UIViewController中拥有一个透明的背景,为此我使用了clearColor,代码如下:

colorPickerVc.view.backgroundColor = UIColor.clearColor()
presentViewController(colorPickerVc, animated: true, completion: nil)

问题在于当colorPickerVc加载完成后,背景颜色变成了黑色。如果可能的话,我希望有一个适用于iOS 7的解决方案。感谢您的帮助。
@good4pc的解决方案:
colorPickerVc.view.backgroundColor = UIColor.clearColor()
if #available(iOS 8.0, *) 
{
    colorPickerVc.modalPresentationStyle = UIModal PresentationStyle.OverCurrentContext
} 
else 
{
     colorPickerVc.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
}

presentViewController(colorPickerVc, animated: true, completion: nil)

我希望你能为我工作,感谢你们的帮助。


你需要添加前一个视图控制器的截图,并将其作为呈现视图的背景。 - iSashok
你是否使用Storyboard来实例化你的ColorPickerVC? - Zell B.
从nib中加载,是的。 让colorPickerVc = ColorPickerViewContro(nibName:"ColorPickerViewContro", bundle: nil) - tamtoum1987
请看这个链接:https://dev59.com/pWgu5IYBdhLWcg3wkHyP#11252969 希望对你有所帮助。 - iOS Geek
5个回答

6
colorPickerVc.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
colorPickerVc.view.backgroundColor = UIColor.clearColor()

是的,这个解决方案很好,但如何让它在iOS 7上工作? - tamtoum1987
1
请添加以下代码:if #available(iOS 8.0, *) { colorPickerVc.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext } else { colorPickerVc.modalPresentationStyle = UIModalPresentationStyle.currentContext } - good4pc
它似乎可以工作,我无法在版本7的设备上测试,但在其他设备上可以工作。谢谢。 - tamtoum1987

0
尝试将视图的层不透明度设置为零,而不是使用清除颜色。

0

clearColor()函数实际上是有效的,但如果您在元素后面没有任何东西,那么使用clearColor()函数屏幕将会变成黑色,因为当没有内容显示时它就是这样的。

您是否确实在对象后面有任何内容?

背景应该是什么颜色、图像或其他东西?

由于它是一个颜色选择器,我想您可能希望最后一个视图控制器在后台运行,但您正在创建一个全新的屏幕,所以做法可能是不要将选择器作为视图控制器,而只是作为UIView放置在前一个视图控制器上方,或者如评论中提到的那样,获取屏幕截图并将其放在后面。


0

调试时查看colorPickerVc.view.superViewcolorPickerVc.view.superView.superView的内容。当呈现视图控制器时,不仅会添加视图到层次结构中。


0
我在几个视图中遇到了同样的问题,解决方法是在Storyboard中将opaque属性设置为NO(未选中)。 (使用Xcode 9 beta)

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