xcode 如何为 UIView 创建半透明背景?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6655093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you create a semi-transparent background for a UIView?
提问by ChrisP
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.
我正在创建一个 UIView,其中包含一些部分覆盖 UIImageView 的文本。我希望用户能够阅读文本并仍然保持对下面图像的透视。我已经尝试将背景颜色设置为 [UIColor clearColor],但是背景是完全透明的,并且根据图像颜色很难阅读文本。
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.
如果我降低 view.alpha=0.5 整个视图包括文本是部分透明的。我想要的是保留文本并部分降低背景的透明度,让图像显示出来。
回答by Bocaxica
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.
对于那些在故事板或 .xib 中拥有视图的人,您只需在界面构建器中通过在实用程序窗格(右侧窗格)中为视图背景选择选项“清除颜色”来完成。“清除颜色”将为视图提供完全透明的背景。
If you need a background colour that is partially transparent, select the desired background colour with the colour picker and use the Opacity slider at the bottom to set the transparency.
如果您需要部分透明的背景颜色,请使用颜色选择器选择所需的背景颜色,然后使用底部的不透明度滑块设置透明度。
回答by Jesse Naugher
I think what you mean is you want the backgroundColor of your UIView to be semi transparent? If you want white/transparent use this:
我想你的意思是你想让你的 UIView 的 backgroundColor 是半透明的?如果你想要白色/透明使用这个:
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:
否则,如果您希望它是另一种颜色,请使用通用的 UIColor 方法: +colorWithRed:green:blue:alpha:
回答by Devangi Desai
This will work.
这将起作用。
myView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7f];
回答by Walter White
Eventually you already have a color so you could use .colorWithAlphaComponent
like this:
最终你已经有了一种颜色,所以你可以.colorWithAlphaComponent
像这样使用:
let exampleColor = UIColor.blackColor()
overlayView.backgroundColor = exampleColor.colorWithAlphaComponent(0.8)
回答by Dmitry Komin
I believe you should use:
我相信你应该使用:
myView.alpha = myAlphaFloat;
myView.opaque = NO;
回答by ToolmakerSteve
For Xamarin C#, at this time, the visual storyboard does not have the "opacity" slider of Xcode's storyboard mentioned by Bocaxica.
对于Xamarin C#,此时的visual storyboard并没有Bocaxica提到的Xcode的storyboard的“不透明度”滑块。
If you set BackgroundColor
for View nameOfView
in storyboard, then in your view controller's ViewDidLoad
, add this line to set alpha:
如果您在情节提要中设置BackgroundColor
为 View nameOfView
,则在您的视图控制器的 中ViewDidLoad
,添加此行以设置 alpha:
nameOfView.BackgroundColor = nameOfView.BackgroundColor.ColorWithAlpha( 0.7f ); // A value between 0 and 1.
回答by Andrey
Swift 3+
斯威夫特 3+
white half transparent:
白色半透明:
view.backgroundColor = UIColor(white: 1, alpha: 0.5)
or black half transparent:
或黑色半透明:
view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
回答by Amit
For Swift 4+ :
对于 Swift 4+ :
Black translucent view:
黑色半透明视图:
view.backgroundColor = UIColor.black.withAlphaComponent(0.5)