当按下按钮时,如何在自定义/填充的 UIButton 中更改(通过 .configuration?)tintColor?

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

请注意,这个问题是关于tintColor的。这个问题与backgroundColor无关,这是完全不同的。另外,我注意到有很多关于按钮上图像的色调颜色 的 QA。这与此无关。这是一个关于 .tintColor

 的属性 
UIButton
的问题

有一个典型的(现代)按钮,

private lazy var toggle: UIButton = { let v = UIButton(type: .custom) v.configuration = .filled() v.configuration?.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { i in var o = i o.font = UIFont.monospacedSystemFont(ofSize: 12, weight: .medium) return o } v.layer.cornerRadius = 5 v.layer.cornerCurve = .continuous v.layer.borderWidth = 1 v.layer.borderColor = UIColor.systemYellow.cgColor v.clipsToBounds = true v.setTitle("Log", for: []) v.setTitleColor(.systemGray3, for: []) v.tintColor = .systemGray ...
请注意,tintColor 是系统灰色。

按下按钮时,如何设置tintColor 改变的颜色?

为了更好地表达这一点:当状态改变时,如何(最好使用配置)使tintColor改变为其他指定的颜色?

swift uikit uibutton
1个回答
1
投票
好吧,事实证明你根本不是在谈论色调颜色。你说的是背景颜色。

将配置的

background.backgroundColor

 设置为固定颜色,它将保持固定。

为了获得更大的灵活性(例如,按下时显示不同的颜色),请设置配置的

background.backgroundColorTransformer

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