首页 > 解决方案 > 用手指单击或悬停时向 UIButton 添加颜色反应

问题描述

我正在快速创建一个自定义警报视图,因为我想要的设计不同,无论如何一切都很完美,但是我添加的按钮在单击时没有任何识别,没有背景变化,什么都没有。当我单击其中一个时,我希望背景变成灰色,就像 swift 中的其他警报(标准警报)一样,或者当我将手指放在它们上方时。

这是代码:

let window = UIApplication.shared.keyWindow!
        self.alertPopUp10.tag = 31
        self.alertPopUp10.frame.origin.x = 0
        self.alertPopUp10.frame.origin.y = 0
        self.alertPopUp10.frame.size.width = window.bounds.width
        self.alertPopUp10.frame.size.height = window.bounds.height

        let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.dark)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)
        blurEffectView.tag = 121212
        blurEffectView.frame = self.alertPopUp10.bounds

        let cardView = UIView()
        blurEffectView.contentView.addSubview(cardView)

        cardView.translatesAutoresizingMaskIntoConstraints = false
        cardView.frame.size.width = 250
        cardView.frame.size.height = 121
        cardView.tag = 76859
        cardView.frame.origin.x = (self.alertPopUp10.bounds.width / 2) - (cardView.frame.width / 2)
        cardView.frame.origin.y = (self.alertPopUp10.bounds.height / 2) - (cardView.frame.height / 2)

        cardView.backgroundColor = .white
        cardView.layer.cornerRadius = CGFloat(15.0)
        cardView.clipsToBounds = true

        let title = UILabel()
        title.text = "Die Session läuft in 2 Minuten ab, soll sie verlängert werden?"
        title.textAlignment = .center
        title.layoutMargins = UIEdgeInsets(top: 30, left: 5, bottom: 5, right: 5)
        title.frame.size.width = 250
        title.frame.size.height = 70
        title.frame.origin.y = 0
        title.font = .systemFont(ofSize: 15.0)
        title.frame.origin.x = (cardView.bounds.width / 2) - (title.frame.width / 2)
        title.numberOfLines = 2
        title.textAlignment = .center
        cardView.addSubview(title)

        let horizontalLine = UIView(frame: CGRect(x: 0, y: title.frame.maxY, width: cardView.frame.width, height: 1))
        horizontalLine.backgroundColor = .lightGray
        cardView.addSubview(horizontalLine)

        let vertLine = UIView()
        vertLine.frame.size.width = 1
        vertLine.frame.origin.x = (cardView.bounds.width / 2) - (vertLine.frame.width / 2)
        vertLine.frame.origin.y = horizontalLine.frame.maxY
        vertLine.frame.size.height = 50
        vertLine.backgroundColor = .lightGray
        vertLine.translatesAutoresizingMaskIntoConstraints = false
        cardView.addSubview(vertLine)

        let yes = UIButton()
        yes.setTitle("JA", for: .normal)
        yes.tag = 54362
        yes.setTitleColor(UIColor(named: "default"), for: .normal)
        yes.addTarget(self, action: #selector(self.resetToken(_:)), for: .touchUpInside)
        yes.frame.size.width = 124
        yes.frame.size.height = 50
        yes.frame.origin.x = 0
        yes.titleLabel?.font = .boldSystemFont(ofSize: 15.0)
        yes.frame.origin.y = horizontalLine.frame.maxY
        cardView.addSubview(yes)

        let no = UIButton()
        no.setTitle("NEIN", for: .normal)
        no.tag = 45234
        no.setTitleColor(UIColor(named: "default"), for: .normal)
        no.addTarget(self, action: #selector(self.dismissAlert(_:)), for: .touchUpInside)
        no.frame.size.width = 124
        no.frame.size.height = 50
        no.titleLabel?.font = .boldSystemFont(ofSize: 15.0)
        no.frame.origin.x = vertLine.frame.maxX
        no.frame.origin.y = horizontalLine.frame.maxY
        cardView.addSubview(no)

        self.alertPopUp10.addSubview(blurEffectView)

标签: iosswiftxcode

解决方案


因此,您有两个选项可以使按钮动态化:

第一个更简单的选项是将按钮初始化为系统按钮。

像这样:

let button = UIButton(type: .system)

按钮按下时会自动生效。

第二个选项更可定制。

突出显示按钮时更改标题颜色:

let button = UIButton()
button.setTitleColor(.yourColor, for: .highlighted)

可以通过覆盖 UIButton 的 isHidden 属性来更改突出显示时的按钮颜色:

class UIButton {
    override open var isHighlighted: Bool {
        didSet {
            let normalColor = .yourColor
            let highlightedColor = .yourColor
            self.backgroundColor = isHighlighted ? highlightedColor : normalColor
        }
    }
}

但请记住,这将影响您将创建的所有 UIButton。如果您只想影响特定按钮,那么最好创建自定义 UIButton。


推荐阅读