Swift Uiview Opacity Programmatically

Swift UIView Opacity Programmatically

What you are describing is:

view.backgroundColor = UIColor.black.withAlphaComponent(0.75)
view.isOpaque = false

The view must not be marked opaque, as it is not opaque.

Swift UIView background color opacity

You can set background color of view to the UIColor with alpha, and not affect view.alpha:

view.backgroundColor = UIColor(white: 1, alpha: 0.5)

or

view.backgroundColor = UIColor.red.withAlphaComponent(0.5)

Changing the alpha of UIView affects the font opacity

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.

How to make SwiftUI view transparent or semi-transparent inside an UIView?

Because "every hosting controller view by default is opaque" so adding the following to the SwiftUI view solves the issue.

let fooView = Foo()
let controller = UIHostingController(rootView: fooView)
let subview = controller.view!

subview.backgroundColor = .clear // <--- THIS LINE

REFERENCE
How can I make a background color with opacity on a Sheet view?

100% opacity UILabel over a 50% opacity background (UIView?)

Just set the background color to be semitransparent:

view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];

Or, in Swift:

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

Or, Swift 3:

view.backgroundColor = UIColor.black.withAlphaComponent(0.5)

Note that, in this particular case, UIColor(white: 0, alpha: 0.5) is more concise, but colorWithAlphaComponent will work in general.

Swift 5 Xcode 10.3 How to set background transparent

I would like to share what worked for me. I am using Xcode 10.3 and Swift 5

Step 1- In Storyboard I set UIView background custom color black and opacity 35%.

Step 2- Presented the ViewController using the bellow chunk of code.

let popupVc : VCAlertMPIN = self.storyboard?.instantiateViewController(withIdentifier: "VCAlertMPIN") as! VCAlertMPIN
popupVc.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
self.present(popupVc, animated: true, completion: nil)

Sample Image

How to find where UIView has been created in code - swift - Programmatically

That is an extremely good question, but unfortunately the answer is no. There is nothing about the view debugger that tells you how any particular view came into existence.

A good way to track the creation of views might be to put symbolic breakpoints on their designated initializers:

Sample Image

You will break a lot, but each time you do you can look to see if this is the problematic view and then continue. Eventually, it will be! And now you can see who is calling the initializer.

Making UIView transparent

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code

self.opaque = NO;

}
return self;
}

- (void)drawRect:(CGRect)rect
{
[[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.0] setFill];
UIRectFill(rect);
[self pushContext];
UIBezierPath *oval = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
[[UIColor redColor] setFill];
[oval fill];

[oval addClip];

[self popContext];
}

Setting alpha on UIView sets the alpha on its subviews which should not happen

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.



Related Topics



Leave a reply



Submit