如果您运行以下 UIKit 应用程序并点击按钮,您可以看到,只有按住它一会儿,它才会更新其颜色,而不是立即更新(如第二个应用程序中所示)(iOS 17.5、iPhone 15 Pro)模拟器,Xcode 15.4)。
此应用程序由一个视图控制器和一个带有一个单元格的表格视图组成,该视图控制器有一个
CheckoutButton
实例约束到其 contentView
顶部、底部、前导和尾随锚点。
结帐按钮使用 UIButton.Configuration 设置其外观,并根据其状态更新它。
import UIKit
class ViewController: UIViewController {
let tableView = UITableView()
let checkoutButton = CheckoutButton()
override func viewDidLoad() {
super.viewDidLoad()
// table view setup
view.addSubview(tableView)
tableView.frame = view.bounds
tableView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.contentView.addSubview(checkoutButton)
checkoutButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
checkoutButton.topAnchor.constraint(equalTo: cell.contentView.topAnchor),
checkoutButton.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor),
checkoutButton.leadingAnchor.constraint(equalTo: cell.contentView.leadingAnchor),
checkoutButton.trailingAnchor.constraint(equalTo: cell.contentView.trailingAnchor)
])
return cell
}
}
class CheckoutButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
var configuration = UIButton.Configuration.plain()
var attributeContainer = AttributeContainer()
attributeContainer.font = .preferredFont(forTextStyle: .headline)
attributeContainer.foregroundColor = .label
configuration.attributedTitle = .init("Checkout", attributes: attributeContainer)
self.configuration = configuration
let configHandler: UIButton.ConfigurationUpdateHandler = { button in
switch button.state {
case .selected, .highlighted:
button.configuration?.background.backgroundColor = .systemCyan
case .disabled:
button.configuration?.background.backgroundColor = .systemGray4
default:
button.configuration?.background.backgroundColor = .systemBlue
}
}
self.configurationUpdateHandler = configHandler
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
在第二个应用程序中,按钮的选择会立即反映在其外观中:
import UIKit
class ViewController: UIViewController {
let button = CheckoutButton()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
button.centerYAnchor.constraint(equalTo: view.centerYAnchor),
button.widthAnchor.constraint(equalToConstant: 300),
button.heightAnchor.constraint(equalToConstant: 44)
])
}
}
class CheckoutButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
var configuration = UIButton.Configuration.plain()
var attributeContainer = AttributeContainer()
attributeContainer.font = .preferredFont(forTextStyle: .headline)
attributeContainer.foregroundColor = .label
configuration.attributedTitle = .init("Checkout", attributes: attributeContainer)
self.configuration = configuration
let configHandler: UIButton.ConfigurationUpdateHandler = { button in
switch button.state {
case .selected, .highlighted:
button.configuration?.background.backgroundColor = .systemCyan
case .disabled:
button.configuration?.background.backgroundColor = .systemGray4
default:
button.configuration?.background.backgroundColor = .systemBlue
}
}
self.configurationUpdateHandler = configHandler
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
这个应用程序由一个只有一个按钮的视图控制器组成:与第一个应用程序不同,没有表格视图。
如何让按钮在点击后立即显示其选择,无论它是在表格视图单元格中还是单独存在?
delaysContentTouches
属性设置为 false
。