92

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?

Naresh
  • 16,698
  • 6
  • 112
  • 113
Avba
  • 14,822
  • 20
  • 92
  • 192
  • 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 08 '13 at 09:09
  • Maybe the order of adding subviews and setting alpha is important. Try playing with different sequences. – Joride Sep 08 '13 at 08:45
  • 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 05 '14 at 13:07

10 Answers10

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.

Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
  • 22
    +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. – Bjorn Roche Mar 12 '14 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 '14 at 06: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 '15 at 03:17
43

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]];

Anooj VM
  • 2,593
  • 22
  • 19
32

In swift

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

UPDATED FOR SWIFT 3

view.backgroundColor = UIColor.white.withAlphaComponent(0.5)
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!!!

MRizwan33
  • 2,723
  • 6
  • 31
  • 42
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 %.

Ralf Hundewadt
  • 1,078
  • 1
  • 13
  • 25
  • 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 '16 at 05: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

Ankit Kumar Gupta
  • 3,994
  • 4
  • 31
  • 54
  • 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 '17 at 06: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 – Ankit Kumar Gupta Jun 16 '17 at 09: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 '17 at 09:35
  • are you using xib or storyboard? – Ankit Kumar Gupta Jun 16 '17 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 '17 at 12:24
  • i am using xib . and used this kinda methodology earlier . – Moxarth Jun 16 '17 at 12:26
  • this code is not working . we can not see pushed view as transparent . did anyone has any other idea ? – Moxarth Jun 28 '17 at 12:04
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)
Naresh
  • 16,698
  • 6
  • 112
  • 113
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.

likid1412
  • 963
  • 8
  • 15
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.

Purnendu roy
  • 818
  • 8
  • 15
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.

Danyun Liu
  • 3,072
  • 24
  • 22