Swift Setting子类UIButton ButtonType

问题描述 投票:0回答:1

我有一个简单的UIButton子类

class IconButton: UIButton {

    init(type: FontAwesomeStyle, icon: FontAwesome, color: UIColor = .black, size: CGFloat = 20) {
        super.init(frame: CGRect.zero)

        let iconAsText = String.fontAwesomeIcon(name: icon)
        let iconText = NSMutableAttributedString(string: iconAsText, attributes: [
            NSAttributedString.Key.font: UIFont.fontAwesome(ofSize: size, style: type)
        ])

        setAttributedTitle(iconText, for: .normal)
        setTitleColor(color, for: .normal)

    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

我遇到的问题是我希望按钮具有系统按钮所具有的行为。具体来说,当您按住按钮时,它会更改颜色。

let button = UIButton(type: .system)

由于buttonType是UIButton的get属性,UIButton.init(type: UIButton.ButtonType)是便利的实现,所以我不确定如何实现此子类。

ios swift uibutton swift4
1个回答
0
投票

仍然不确定是否可以在子类中复制.system按钮类型,但是获得我想要的行为的解决方案如下:

© www.soinside.com 2019 - 2024. All rights reserved.