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 underneath. I have tried setting the background color to [UIColor clearColor], but then the background is totally transparent and it is hard to read the text depending upon the image colors.
If I lower the view.alpha=0.5 the whole view including the text is partially transparent. What I'd like is to maintain the text and reduce the transparency of the background partially, allowing the image to show through.
Changing the opacity of the background color only To achieve this, use a color value which has an alpha channel—such as rgba. As with opacity , a value of 1 for the alpha channel value makes the color fully opaque. Therefore background-color: rgba(0,0,0,. 5); will set the background color to 50% opacity.
use opacity css property. Do you want a semi transparent background color or image? Add this to your CSS background-color:rgba(255,0,0,0.5) . Increase or decrease the 0.5 to lower or raise the opacity.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With