当使用 UIButtonConfiguration 的 UIButton 突出显示状态处于活动状态时,如何禁用标题颜色更改?

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

这就是我的 UIButton 的定义和初始化方式:

lazy var addButton: UIButton = {
    let button = UIButton(radius: 32, title: "+", font: .poppinsRegular.withSize(40), backgroundColor: .purple)
    button.menu = UIMenu(title: "")
    button.showsMenuAsPrimaryAction = true
    return button
}()


extension UIButton {
    convenience init(
        radius: CGFloat = 0,
        title: String? = nil,
        backgroundColor: UIColor? = nil,
        foregroundColor: UIColor = .white,
        font: UIFont? = nil
    ) {
        var configuration = UIButton.Configuration.filled()
        configuration.baseForegroundColor = foregroundColor
        configuration.title = title
        configuration.background.cornerRadius = radius
        configuration.cornerStyle = .fixed
        configuration.contentInsets = .zero
        configuration.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
            var outgoing = incoming
            outgoing.font = font
            return outgoing
        }
        configuration.baseBackgroundColor = backgroundColor
        self.init(configuration: configuration)
    }
}

enter image description here

enter image description here

ios swift uibutton
1个回答
-2
投票

我相信玩具需要使用设置图像

setBackgroundImage(_ image: UIImage?, for state: UIControl.State)

并将其设置为 .normal 和 .highlighted / .selected 状态,这样它就不会使用默认实现

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