Alpha for Background Color Not Working in iOS

alpha for background color not working in iOS

If you are using storyboard and present view controller using segue, the presentation attribute of segue that show you view controller with transparent background should be over full screen as shown in attachment.

Sample Image

Then in your view controller set the background color by using this :

self.view.backgroundColor = UIColor(white: 0, alpha: 0.4)

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)

How to set the alpha of the background color of a UIButton?

To set the color with alpha use:

UIColor(red: 0.5, green: 0.5, blue:0, alpha: 1.0)

or:

 button.backgroundColor?.withAlphaComponent(0.5)

Setting a background color with alpha in Navigation bar

If you just want the NavigationBar (excluding status bar) background with alpha.. Try this

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.backgroundColor = UIColor.blue.withAlphaComponent(0.5)
}

If you want background color with alpha including status bar, Im afraid you have to use an image with alpha value. see this

Swift 4 backgroundColor bug?

Found the problem:
When instantiating a variable which holds the view before displaying it, the background color of this view is white. The debugger calls this UICachedDeviceWhiteColor.

Why would you do that?
If you instantiate the view before it is displayed, you can switch with a menu between different views very fast with "bringSubview(toFront: ...).

How to fix:
To fix this you have to instantiate the view every time you want to show it.

Hope I could help some lost souls with this answer and Apple is going to fix this bug.

How to change the alpha channel of an existing background color

You're looking for something like this:

CGFloat newAlpha = 0.7;
CGColorRef color = CGColorCreateCopyWithAlpha(self.yourTextField.layer.backgroundColor, newAlpha);
[self.yourTextField.layer setBackgroundColor:color];

Or if you already know the color and just want the current alpha:

CGFloat alpha = CGColorGetAlpha(self.yourTextField.layer.backgroundColor); //Get current alpha       
self.yourTextField.layer.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:alpha].CGColor;

UIView background color with alpha in xib

The issue was in custom show method. It didn't use presentViewController:animated:completion: method. It used alpha animaiton from 0 to 1. The second - when using standard presentViewController:animated:completion: method cocoa optimization was hiding the underlying view, so I need to set up (for iOS 8)

vc_to_present_on.modalPresentationStyle = UIModalPresentationCurrentContext;
vc_to_be_presented.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

And then everything work as expected



Related Topics



Leave a reply



Submit