I am creating a UIView containing some text that partially covers a UIImageView. I want the user to be able to read the text and still maintain a perspective on the image undern
For those who have their view in a storyboard or .xib, you simply do it in interface builder by selecting the option "Clear Color" for the Background of the view in the Utilities Pane (the pane on the right). "Clear Color" will give the view a completely transparent background.
If you need a background color that is partially transparent, select the desired background color with the color picker and use the Opacity slider at the bottom to set the transparency.
Another very useful option is to add colors to your .xcassets
library, so that you can use the same color easily in different views. You can make these colors (semi-)transparent as well, here's how:
.xcassets
libraryAttributes Inspector
you can then change the color and use the slider to adjust its opacityBackground
option of in the Attributes Inspector
you can now select the Color you added to your .xcassets
library. This is very useful if you have multiple views across your app using the same background.In code you can access the colors from your Color Assets using:
SWIFT (UIColor): UIColor(named: "DP Textfield")
SWIFTUI (Color): Color("DP Textfield")
This will work.
myView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7f];
For Swift 4+ :
Black translucent view:
view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
I believe you should use:
myView.alpha = myAlphaFloat;
myView.opaque = NO;
Eventually you already have a color so you could use .colorWithAlphaComponent
like this:
let exampleColor = UIColor.blackColor()
overlayView.backgroundColor = exampleColor.colorWithAlphaComponent(0.8)
For Xamarin C#, at this time, the visual storyboard does not have the "opacity" slider of Xcode's storyboard mentioned by Bocaxica.
If you set BackgroundColor
for View nameOfView
in storyboard, then in your view controller's ViewDidLoad
, add this line to set alpha:
nameOfView.BackgroundColor = nameOfView.BackgroundColor.ColorWithAlpha( 0.7f ); // A value between 0 and 1.