Changing the alpha of UIView affects the font opacity - ios

Quite simple question: I want to change the opacity of UIView and keep font above it with alpha = 1.0.
So as you can see there are two labeles and under them is UIView. I've set the opcaity of the UIView (for example: #IBOutlet weak var coView: UIView! self.coView.alpha = 0.8). The result is that both UIView and labels are more transparent and I want to keep label's text on alpha = 1.0. Is it possible to do without changing stack views and constraints?

Use
coView.backgroundColor = UIColor.black.withAlphaComponent(0.8)
It's super annoying and not precisely what you wanted, but it's a way around it. It sets the background color of the coView with an alpha value, which effectively gives you what you want.

Instead of changing the Alpha of the Object you should leave the alpha at 1 and choose a custom Background colour (in the Attributes inspector):
Then in the custom colour widget change the opacity of the colour as desired.

Changing the alpha of the UIView changes how all its subviews are seen to the new alpha set. There no way to actually avoid this, but you can go around this by using some other auxiliary views.
For example, inside your UIView pm, you could have another subView called backgroundView, which is on the back and that's the one you set the background color. Every other element inside your UIView pm would be on top of the backgroundView with a clear background color.
If you want to only change the alpha of the color, you just change the alpha to the backgroundView and the other elements in UIView pm will remain with the same alpha

Related

Set the background of UILabel to transparent black

I have a UILabel that whose background I want to set to black with alpha component so that this label can have a transparent black background.
In the storyboard when I set the background to the colour I want with an alpha component even the text of the label that is in white colour becomes lighter with the alpha component.
To overcome this problem I embedded the label in a UIView and even then the same problem persists.
How can I overcome this problem. Any help will be appreciated.
You can Do only With label make IBObject an write following line.
label.backgroundColor = UIColor.black.withAlphaComponent(0.4)
Instead of changing the alpha value, change the opacity of the label background color in storyboard.
For better understanding, I have taken an imageView and placed a label at the bottom of imageView and changed the background color of the label and textColor to white. At this stage you can't see the image behind the label.
Now, go to attributes inspector and select the label background color. There you can see the opacity and change it's value. Here I have made it 50%, you can change it to whatever you want between 0 to 100 until you get your desired transparency.
create the UIview with black background color and add the UIlabel inside the view and set the alpha to 0.5 of view . finally you get the black transparent, for e.g
if you need the label text in bold change the view hierarchy
place the UILabel fist then add the UIView.
Place the UILabelabove the UIView, not inside it.
Assume that we are talking about v1 the view that contains your label , if v1 is over another view that has bright colors then when your v1's alpha get lower your label white text color will become harder to see , the text color has nothing to do with the background alpha component , so you need to reconsider using the white color or the alpha component background or the view that is behind v1 make it darker that's all we can say

Original UILabel for transparent view with opacity alpha 0.5

I have a CollectionView cell with labels, I want to make that view transparent with alpha 0.5 but not the UILabel's. I have a content view with clear background, and for the view above the content view, given with the background white with alpha value 0.5. The CollectionView is transparent but my problem is that the labels are also slightly transparent it's not much clear. please help me if anyone knows the solution.
You can achive this effect by putting your label into a view and then changing alpha component of view itself like this:
alphaView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.5)
Label shouldn't be transparent then. Ofc you need to set your label background color to clear.
You should not set the alpha value of the view. If you set the alpha value of a view this value takes effect on all subviews, too. You should set the alpha value of the background color of the view.
You should not set alpha value.
Instead you can set the background colour of the uicollectionviewcell to be clear colour. In this way your collection view cell will be transparent.
Then you could give whatever color to the labels.

UILabel text color is light

In UIView i have added a label , who's text color is white but when I changes the Alpha of the UIView to 0.25 the color of UILabel lighten If I change Alpha back to 1.0 the UILabel text color result is what I expect.
I have no clue how to change setup.
Screenshot is included.
Also the Alpha is same for the Toolbar and the result is same , icon image of button is also light.
It is normal behaviour because if you change alpha of the parent view it will be propagated to the child views. You can maybe change not the parent view alpha but use colorWithAlphaComponent for this view background color.
e.g.
yourParentView.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0.5)

CGContextClearRect ignores alpha value set in storyboard

I'm using CGContextClearRect method in MyView's drawRect to clear MyView to background color. However, I must explicitly set the background color alpha value of MyView in code. If I set the alpha value in storyboard, the CGContextClearRect makes MyView black. So why the alpha value set in storyboard doesn't do the trick?
The drawRect method allows you to reset the view's layer content when flagged for a refresh. There's is no need to call CGContextClearRect as clearing the layer is automatically managed by the system before it calls the drawRect method, effectively providing you with a blank canvas for the view's layer.

I want transparent UIView but not the elements which I added over it

First of all sorry for the Title. I didn't knew how to exactly convey my problem. So my problem is that I have set black color in the background of a UIView and I've made it transparent. But the UI elements which I am adding over it (e.g Buttons, Labels etc) is also getting faded along with the UIView. For example if I set the alpha of UIView to be 0.5 then it automatically applies to all the controls I've added on that UIView which I don't want. So please tell me how to save elements from getting faded.
If you just have to make view transparent not the subviews, then go with
self.view.backgroundColor=[UIColor clearColor];
What it does is, it will make background view transparent, instead setting alpha property will fade subviews also.
instead of writing in code. we can set it in Interface Builder also. By selecting the background color in the Attributes Inspector in the IB you have to click on background color property and select other and there you can set the alpha or any color you want directly which reduces the code.
#Rahul
You can use something like:
view.backgroundColor=[[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:0.5];
This will keep the transparency solely to the background but not the subviews.
What you need to do is, don't change the alpha of the UIView, instead, set backgroundColor with the desired transparency, e.g. if you want to have half transparent black, do view.backgroundColor = [UIColor colorWithWhite:0. alpha:.5];
Good Luck!

Resources