How do you create a semi-transparent background for a UIView?
OPTION 1 - USING STORYBOARDS
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.
OPTION 2 - USING COLOR ASSETS (AND STORYBOARDS)
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:
- Open your
.xcassets
library - Add a Color Set
- Give it a useful name and select the color thumbnail
- In the
Attributes Inspector
you can then change the color and use the slider to adjust its opacity
- Go back to your storyboard and select the view you need this transparent background color
- In the
Background
option of in theAttributes 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")
I think what you mean is you want the backgroundColor of your UIView to be semi transparent? If you want white/transparent use this:
myView.backgroundColor = [UIColor colorWithWhite:myWhiteFloat alpha:myAlphaFloat];
else if you want it to be another color use the general UIColor method: +colorWithRed:green:blue:alpha: