设置UIButton背景的alpha但不是标题

时间:2021-01-12 00:21:31

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?

6 个解决方案

#1


23  

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)

#2


15  

You can also change the alpha of the color in the storyboard,

您还可以更改故事板中颜色的alpha值,

see attached image:

见附图:

设置UIButton背景的alpha但不是标题

#3


7  

In Swift:

在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)
    }

}

#4


1  

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;
    }
}

#5


0  

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,您可以通过将按钮样式设置为Custom而不是Round Rect来删除背景。这使标题保持不变,但删除了按钮背景。如果您在Storyboards中执行此操作,则可以更改元素属性中的按钮样式。对于代码,格式为:

UIButton *button = [[UIButton alloc] buttonWithType:UIButtonTypeCustom];
// Setup frame and add to view

#6


-1  

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会影响所有子视图。

#1


23  

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)

#2


15  

You can also change the alpha of the color in the storyboard,

您还可以更改故事板中颜色的alpha值,

see attached image:

见附图:

设置UIButton背景的alpha但不是标题

#3


7  

In Swift:

在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)
    }

}

#4


1  

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;
    }
}

#5


0  

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,您可以通过将按钮样式设置为Custom而不是Round Rect来删除背景。这使标题保持不变,但删除了按钮背景。如果您在Storyboards中执行此操作,则可以更改元素属性中的按钮样式。对于代码,格式为:

UIButton *button = [[UIButton alloc] buttonWithType:UIButtonTypeCustom];
// Setup frame and add to view

#6


-1  

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会影响所有子视图。