tamtoum1987
tamtoum1987

Reputation: 2057

clearColor make background color black

I want to have a tranparent background in my UIViewController for that I used clearColor like this

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

the problem is when colorPickerVc finished loading, the background color become black I want a solution if possible that work on ios 7 to thank's for your help


Solution of @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)

work for me, thank you guys for your help

Upvotes: 5

Views: 4541

Answers (5)

Rom
Rom

Reputation: 143

I had the same problem with several views and all I had to do is set the opaque trait to NO (unselected) in the storyboard. (using Xcode 9 beta)

Upvotes: 0

The clearColor() is actually working, but if you don't have anything behind the element you make clearColor() the screen will be black because thats how it looks when there is nothing to show

Do you actually have anything behind the object you make clear?

What color or image or whatever should be in the background?

As it is a color-picker i'd assume that you want the last viewController in the background, but what you're doing is creating a whole new screen, so the way to do it would probably be to not have the picker as a viewController but just as a UIView to put as an overlay on the previous viewController or as was mentioned in the comments, get a screenshot and put it behind

Upvotes: 0

manta
manta

Reputation: 369

See in debug what colorPickerVc.view.superView and colorPickerVc.view.superView.superView is. When presenting a view controller it's not only the view that is added to the hierarchy.

Upvotes: 0

good4pc
good4pc

Reputation: 711

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

Upvotes: 6

jp2g
jp2g

Reputation: 692

Try setting the view's layer's opacity to zero instead of using clear color

Upvotes: 0

Related Questions