简体   繁体   English

UIButton在水龙头Swift上不突出显示

[英]UIButton not highlighting on tap Swift

Introduction 介绍

I created a button and added subviews to it / gave it a custom look. 我创建了一个按钮并为其添加了子视图/为其提供了自定义外观。

As subclassing buttons is a lot of pain, I decided to do it (yeah, I know it's bad) inside my view controller and creating an extension for UIButton which styles create buttons. 由于子类化按钮非常麻烦,因此我决定在视图控制器内部进行操作(是的,我知道这很不好),并为UIButton创建了一个扩展,以样式创建按钮。

The button looks like this: 该按钮如下所示:

我的按钮

I set up the subviews as follows: 我将子视图设置如下:

func styleCreateButton(serviceFee: Float) {
        let feeLabel: DefaultLabel = {
            let lbl = DefaultLabel(labelFont: UIFont(name: "MyCustomFont", size: 12)!, labelTextColor: ColorCodes.textSuperLightGray, labelText: "$\(serviceFee) Service Fee")

            return lbl
        }()

        let createLabel: DefaultLabel = {
            let lbl = DefaultLabel(labelFont: UIFont(name: "MyCustomFont", size: 15)!, labelTextColor: .white, labelText: "Create")

            return lbl
        }()

        let arrowRight: UIButton = {
            let btn = UIButton(type: .system)
            btn.setImage(UIImage(named: "left-arrow")?.rotate(radians: CGFloat(180).degreesToRadians).resizedImage(newSize: CGSize(width: 20, height: 20)).withRenderingMode(.alwaysTemplate), for: .normal)
            btn.contentMode = .center
            btn.tintColor = .white

            return btn
        }()

        self.addSubview(feeLabel)
        feeLabel.anchor(top: nil, left: self.leftAnchor, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 16, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
        feeLabel.sizeToFit()
        feeLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true

        self.addSubview(arrowRight)
        arrowRight.anchor(top: nil, left: nil, bottom: nil, right: self.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 16, width: 20, height: 20)
        arrowRight.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true

        self.addSubview(createLabel)
        createLabel.anchor(top: nil, left: nil, bottom: nil, right: arrowRight.leftAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 12, width: 0, height: 0)
        createLabel.sizeToFit()
        createLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    }

The button itself was declared as follows: 该按钮本身声明如下:

private lazy var createButton: UIButton = {
        let btn = UIButton(type: .system)

        btn.backgroundColor = ColorCodes.logoPrimaryColor

        btn.layer.shadowOffset = CGSize(width: 0, height: 5)
        btn.layer.shadowColor = UIColor.black.cgColor
        btn.layer.shadowOpacity = 0.2
        btn.layer.shadowRadius = 5

        return btn
    }()

Also, the button has a target and I have no idea why it does not highlight in the typical iOS way when clicking. 另外,该按钮有一个目标,我不知道为什么单击时按钮没有以典型的iOS方式突出显示。 Right now, the handler function gets called, but the button does not highlight. 现在,处理程序函数被调用,但是该按钮未突出显示。

I would appreciate any help :) 我将不胜感激任何帮助 :)

try to do this 尝试这样做

class HighlightedButton: UIButton {
    override var isHighlighted: Bool {
        didSet {
            backgroundColor = isHighlighted ? .blue : .white
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Swift中点击并按住UIButton? - Tap and Hold UIButton in Swift? 在 Storyboard 和 Swift 2 中禁用 UIButton 突出显示 - Disable UIButton highlighting in Storyboard and Swift 2 斯威夫特| UIButton 导致点击崩溃 - Swift | UIButton causes crash on tap 在Swift中将ImageView设置为UIButton的当前图像 - Set ImageView to current image of UIButton on tap in Swift UIButton在iOS7中点击时未显示突出显示-Swift - UIButton not showing highlight on tap in iOS7 - Swift 通过单击同一单元格SWIFT中的UIButton访问自定义单元格对象属性 - Accessing custom cell object properties on tap of UIButton in same cell SWIFT 当我点按1个UIButton swift时,停止所有UIButtons的工作 - Stop all UIButtons from working when I tap 1 UIButton swift iOS Swift 3 - UICollectionViewCell中的UIStackView上的UIButton没有响应tap事件(没有交互) - iOS Swift 3 - UIButton on UIStackView in UICollectionViewCell is not responding to tap events (no interaction) UIKit-Swift 自定义 UIButton 不会触发点击动作 - UIKit-Swift custom UIButton does not trigger action on tap UIButton子类突出显示错误(即使点击或按下后,突出显示仍保持或持续存在) - UIButton subclass highlighting error (highlight remains or persists even after tap or touch down)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM