5

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

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
tamtoum1987
  • 1,957
  • 3
  • 27
  • 56
  • you need add screenshot of previous view controller and add it as a background in presented – iSashok Jul 21 '16 at 12:23
  • Do you use storyboard to instantiate your colorPickerVC ? – Zell B. Jul 21 '16 at 12:25
  • From a nib yes let colorPickerVc = ColorPickerViewContro(nibName:"ColorPickerViewContro", bundle: nil) – tamtoum1987 Jul 21 '16 at 12:30
  • Have a look at this : http://stackoverflow.com/questions/11236367/display-clearcolor-uiviewcontroller-over-uiviewcontroller/11252969#11252969 Hope it helps you. – iOS Geek Jul 21 '16 at 12:31

5 Answers5

6
colorPickerVc.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
colorPickerVc.view.backgroundColor = UIColor.clearColor()
good4pc
  • 711
  • 4
  • 17
  • yes this solution work fine, but how make this work on ios 7 ? – tamtoum1987 Jul 21 '16 at 12:31
  • 1
    add the following code if #available(iOS 8.0, *) { colorPickerVc.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext } else { colorPickerVc.modalPresentationStyle = UIModalPresentationStyle.CurrentContext } – good4pc Jul 21 '16 at 12:33
  • it seems to work i cant test on device version 7 but it works in others thanks – tamtoum1987 Jul 21 '16 at 12:36
0

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

jp2g
  • 682
  • 4
  • 8
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

0

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.

manta
  • 369
  • 1
  • 9
0

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)

Rom
  • 143
  • 1
  • 8