xcode 设置 UIButton 背景的 alpha 但不是标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17492840/
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
Set alpha of background of UIButton but not the title
提问by Jon
When I set the alpha of the button it also affects the opacity of the title. Is there a way to target only the background and leave the title alpha at 1.0?
当我设置按钮的 alpha 时,它也会影响标题的不透明度。有没有办法只定位背景并将标题 alpha 保留在 1.0?
回答by SasaT
This works for me:
这对我有用:
self.myButton.backgroundColor = [UIColor clearColor];
or if you don't want to have it completely clear, but still with transparency you can use:
或者,如果您不想让它完全清楚,但仍然具有透明度,您可以使用:
self.myButton.backgroundColor = [UIColor colorWithRed:200.0/255.0 green:200.0/255.0 blue:200.0/255.0 alpha:0.5];
The second example would give you gray color with alpha 0.5.
第二个示例将为您提供 alpha 为 0.5 的灰色。
SWIFT 4 Update
SWIFT 4 更新
myButton.backgroundColor = .clear
OR
或者
myButton.backgroundColor = UIColor(red: 200.0/255.0, green: 200.0/255.0, blue: 200.0/255.0, alpha:0.5)
回答by brush51
回答by King-Wizard
In Swift:
在斯威夫特:
import UIKit
class SignInUpMenuTableViewControllerBigButton: UIButton {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.applyCustomDesign()
}
func applyCustomDesign() {
// We set the background color of the UIButton.
self.clearHighlighted()
}
override var highlighted: Bool {
didSet {
if highlighted {
self.highlightBtn()
} else {
self.clearHighlighted()
}
}
}
func highlightBtn() {
self.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.0)
}
func clearHighlighted() {
self.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.05)
}
}
Swift 4.2
斯威夫特 4.2
import UIKit
class SignInUpMenuTableViewControllerBigButton: UIButton {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.applyCustomDesign()
}
func applyCustomDesign() {
// We set the background color of the UIButton.
self.clearHighlighted()
}
override var highlighted: Bool {
didSet {
if highlighted {
self.highlightBtn()
} else {
self.clearHighlighted()
}
}
}
func highlightBtn() {
self.backgroundColor = UIColor.whiteColor().withAlphaComponent(0.0)
}
func clearHighlighted() {
self.backgroundColor = UIColor.whiteColor().withAlphaComponent(0.05)
}
}
回答by lakesh
Subclassing UIButton and extending the setEnabled: method seems to work:
子类化 UIButton 并扩展 setEnabled: 方法似乎有效:
- (void) setEnabled:(BOOL)enabled {
[super setEnabled:enabled];
if (enabled) {
self.imageView.alpha = 1;
} else {
self.imageView.alpha = .25;
}
}
回答by m.alqadi
Swift button.backgroundColor = UIColor.white.withAlphaComponent(0.7)
迅速 button.backgroundColor = UIColor.white.withAlphaComponent(0.7)
回答by Alex Blundell
With a UIButton, you can remove the background by setting the button style to Custom rather than Round Rect. That keeps the title intact, but the button background removed. If you're doing this in Storyboards, you can change the button style in the element properties. For code, the format is:
使用 UIButton,您可以通过将按钮样式设置为自定义而不是圆形来移除背景。这使标题保持不变,但删除了按钮背景。如果您在 Storyboards 中执行此操作,则可以更改元素属性中的按钮样式。对于代码,格式为:
UIButton *button = [[UIButton alloc] buttonWithType:UIButtonTypeCustom];
// Setup frame and add to view
回答by Fogmeister
Are you just using a plain colour for the background? Or are you using an image?
你只是使用纯色作为背景吗?或者你在使用图像?
If you're using an image then you could use an image with the alpha built in.
如果您使用的是图像,那么您可以使用内置 alpha 的图像。
If you're using a colour then you could set the alpha on the colour.
如果您使用的是颜色,那么您可以在颜色上设置 alpha。
However, changing the alpha of any view affects all sub views.
但是,更改任何视图的 alpha 会影响所有子视图。