Ios 设置UIButton背景的alpha,但不设置标题

Ios 设置UIButton背景的alpha,但不设置标题,ios,xcode,uibutton,Ios,Xcode,Uibutton,当我设置按钮的alpha时,它也会影响标题的不透明度。有没有办法只针对背景,将标题alpha保留为1.0?将UIButton子类化并扩展setEnabled:方法似乎有效: - (void) setEnabled:(BOOL)enabled { [super setEnabled:enabled]; if (enabled) { self.imageView.alpha = 1; } else { self.imageView.al

当我设置按钮的alpha时,它也会影响标题的不透明度。有没有办法只针对背景,将标题alpha保留为1.0?

将UIButton子类化并扩展setEnabled:方法似乎有效:

- (void) setEnabled:(BOOL)enabled {    
    [super setEnabled:enabled];
    if (enabled) {
        self.imageView.alpha = 1;
    } else {
        self.imageView.alpha = .25;
    }
}

你只是用普通的颜色作为背景吗?或者你正在使用图像

如果您使用的是图像,那么您可以使用内置alpha的图像

如果您使用的是颜色,则可以设置颜色的alpha


但是,更改任何视图的alpha都会影响所有子视图。

使用UIButton,可以通过将按钮样式设置为“自定义”而不是“圆形矩形”来删除背景。这将保持标题不变,但按钮背景已删除。如果在故事板中执行此操作,则可以在元素属性中更改按钮样式。对于代码,格式为:

UIButton *button = [[UIButton alloc] buttonWithType:UIButtonTypeCustom];
// Setup frame and add to view
这对我很有用:

self.myButton.backgroundColor = [UIColor clearColor];
或者,如果您不想让它完全清晰,但仍然具有透明度,您可以使用:

self.myButton.backgroundColor = [UIColor colorWithRed:200.0/255.0 green:200.0/255.0 blue:200.0/255.0 alpha:0.5];
第二个示例将为您提供alpha为0.5的灰色

SWIFT 4更新

myButton.backgroundColor = .clear


在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

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

}

您还可以更改情节提要中颜色的alpha

见附图:


Swift
button.backgroundColor=UIColor.white.withAlphaComponent(0.7)

这是否真的适用于您?在iOS 7中,我似乎根本无法更改按钮图像的Alpha设置。为什么我想不起来呢?谢谢加上一个用于带有Alpha组件的
颜色
!对于Swift 3:
UIColor.black.withAlphaComponent(0.8)
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)
    }

}