93

According to the documentation for UIVIew @property(nonatomic) CGFloat alpha

The value of this property is a floating-point number in the range 0.0 to 1.0, where 0.0 represents totally transparent and 1.0 represents totally opaque. This value affects only the current view and does not affect any of its embedded subviews.

I have a container view configured as follows:

self.myView.backgroundColor = [UIColor blackColor];
self.myView.alpha = 0.5;
[self addSubview:self.myView];

And then add subviews to 'myView'

[myView addSubView anotherView];
anotherView.alpha = 1;
NSLog(@"anotherView alpha = %f",anotherView.alpha); // prints 1.0000 as expected

But 'anotherView' does have alpha on screen (it is not opaque as expected)

How can this be and what can be done?

3
  • Maybe the order of adding subviews and setting alpha is important. Try playing with different sequences.
    – Joride
    Sep 8, 2013 at 8:45
  • Add all code of creation of anotherView :) and also i thinks it is typo but are you sure you initialize self.myView ? and add anotherView such like [self.self addSubview:self.myView];
    – iPatel
    Sep 8, 2013 at 9:09
  • 1
    indeed, the documentation is correct: that won't affect the its embedded subviews and the subviews' alpha are always the same – but the rendered views have an alpha value which is all subviews' alpha values, multiplied. e.g. if the subviews alpha 0.8 and the superview's alpha was 1.0, but you change it to 0.6, the subviews alpha is still the same, 0.8. the rendered subview's alpha value is changed only from 0.8 to 0.48.
    – holex
    Aug 5, 2014 at 13:07

10 Answers 10

124

I think this is a bug in the documentation. You should file it at bugreport.apple.com.

Everything I can see after a bit of quick research suggests what you are seeing is how it always has behaved, and my own testing shows it too.

The alpha of a view is applied to all subviews.

Perhaps all you need is [[UIColor blackColor] colorWithAlphaComponent:0.5] but if not you will need to make the view a sibling instead of a child.

3
  • 23
    +1. FWIW, I think the docs are arguably correct, if extremely unclear (to the point of being incorrect for all practical purposes). The alpha of the subviews is affected, but not the alpha value. This is the same behaviour, I think, for the hidden property. Setting hidden hides subviews, but doesn't cause subviews hidden property to be set. Mar 12, 2014 at 17:22
  • 2
    i had requirement to show a view some thing like uipopover but in iphone.I have to create a uiviewcontroller with [[UIColor blackColor] colorWithAlphaComponent:0.5].thanks it worked
    – maddy
    Jul 17, 2014 at 6:34
  • 2
    I don't believe the docs are "arguably correct" at all. In the real world, devs will read the docs and believe that child alphas are unaffected. When, in reality, the composited result will be as if the alphas had been modified. It's just a case of bad documentation.
    – Womble
    Dec 16, 2015 at 3:17
44

Don't set the alpha directly on the parent view. Instead of it use the below line of code which will apply transparency to parentview without affecting its child views.

[parentView setBackgroundColor:[[UIColor clearColor] colorWithAlphaComponent:0.5]];

3
  • this code is not working with latest ios and xcode . anyone has different ideas ?
    – Moxarth
    Jun 28, 2017 at 12:06
  • 1
    Excellent!! For swift 4 use: parentView.backgroundColor=UIColor.black.withAlphaComponent(0.5) Jun 15, 2018 at 7:09
  • Great solution.
    – iLearner
    Mar 5, 2019 at 7:27
32

In swift

view.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.5)

UPDATED FOR SWIFT 3

view.backgroundColor = UIColor.white.withAlphaComponent(0.5)
4
  • 2
    This doesn't address the question asked. Mar 16, 2016 at 2:18
  • This gives me an error of: "Value of type 'UIColor' has no member 'colorWithAlphaComponent'".
    – Krivvenz
    Oct 12, 2016 at 14:23
  • 1
    Hello @Krivvenz, 'colorWithAlphaComponent' has been renamed to 'withAlphaComponent'. See my edited answer. Oct 12, 2016 at 17:50
  • this code is not working . we can not see pushed view as transparent . did anyone has any other idea ?
    – Moxarth
    Jun 28, 2017 at 12:04
27

Set Opacity of the background color instead of alpha will not affect its child views.

  1. select view.
  2. go to attribute inspector than background color
  3. click on "others"
  4. set opacity to 30%

Or you can set by programmetically

var customView:UIView = UIView()
customView.layer.opacity = 0.3

Thats it. Happy Coding!!!

10
  • Is there any way to do this programmatically? Sep 18, 2018 at 0:41
  • @DCIndieDev Sorry i haven't found opacity as a property of UIView. So set by storyboard.
    – MRizwan33
    Sep 19, 2018 at 5:50
  • This is a great answer. Worked great. Sep 27, 2018 at 17:45
  • This should be the accepted answer, Worked as needed. Thanks.
    – Missa
    Sep 28, 2018 at 20:57
  • 1
    @MRizwan33 You can access the opacity property from the [your UIView].layer.opacity
    – Missa
    Oct 16, 2018 at 16:56
17

If you like Storyboards, put a User Defined Runtime Attribute for your view in the Identity Inspector:

Key Path: backgroundColor, Type: Color, Value: e.g. white color with Opacity 50 %.

1
  • Great answer! In the Attributes Inspector I set the view's alpha to one. In the Identity Inspector I insert key path as above with Opacity 50%. The view's opacity is now at 50%, but the opacity of the subviews is still 100%. I suppose that alpha and opacity are NOT one in the same.
    – etayluz
    Aug 17, 2016 at 5:22
6

Simplest solution as discussed is to change the alpha as follows : Updated version for Xcode 8 Swift 3 is :

yourParentView.backgroundColor = UIColor.black.withAlphaComponent(0.4)

Objective C:

yourParentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];

Refer Apple Developer Docs here : https://developer.apple.com/reference/uikit/uiview/1622417-alpha

7
  • yes i applied this , but view did not get transparent . i have used this code earlier many times . but this time its not working . and showing my view as solid black color . so any alternate method or it has changed with new iOS updates ?
    – Moxarth
    Jun 16, 2017 at 6:40
  • Most probably the view behind your yourParentView must be solid black please check your storyboard for xib and also your code that whether it is set to black from any where else. Hope this helps Jun 16, 2017 at 9:12
  • i am just pushing another view from one view . the pushed view would appear transparent after this code . i have used it many times earlier . it is as simple as this . --> settings *set = [[settings alloc] initWithNibName:@"settings" bundle:nil]; --> set.view.backgroundColor = [ [UIColor blueColor] colorWithAlphaComponent:0.3f]; --> [self.navigationController pushViewController:set animated:YES ];
    – Moxarth
    Jun 16, 2017 at 9:35
  • are you using xib or storyboard? Jun 16, 2017 at 11:35
  • but now i am using this code and it is not working . pushed view will appear with solid color whichever color we have given , and not transparent .
    – Moxarth
    Jun 16, 2017 at 12:24
6

In Swift 4.2 and Xcode 10.1

Don't add colour and alpha value through storyboard. Only programmatic approach will work in this case.

transparentView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
1
  • @iOS Thank u , this one helped me.
    – Raghuram
    Sep 30, 2019 at 6:27
2

Here is a bit complex solution:

UIView *container;
UIView *myView;
UIView *anotherView;

myView.alpha = 0.5;
[container addSubview:myView];

anotherView.alpha = 1;
[container addSubview:anotherView];

Use a container view as superview, anotherView and myView are both subview in container, anotherView is not a subview in myView.

2

For now there is only one way make the Parent View transparent and don't put any child views inside (don't put any views as subview) the parent view, put that child views outside of the parent view. To make parent view transparent you can do this via storyboard.

//Transparent the parentView

parentView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.8)

Put the other view outside of the parent view. It will work like a charm.

1

Please refer to the bold description from Xcode documentation.

The value of this property is a floating-point number in the range 0.0 to 1.0, where 0.0 represents totally transparent and 1.0 represents totally opaque. Changing the value of this property updates the alpha value of the current view only. However, the transparency imparted by that alpha value affects all of the view's contents, including its subviews. For example, a subview with an alpha value of 1.0 that is embedded in a parent view with an alpha value of 0.5, appears onscreen as if its alpha value is also 0.5.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.