Skip to content

Instantly share code, notes, and snippets.

@enomoto
Created June 20, 2023 00:44
Show Gist options
  • Save enomoto/29b420075bd5c7bf07d06724cd2c0fe6 to your computer and use it in GitHub Desktop.
Save enomoto/29b420075bd5c7bf07d06724cd2c0fe6 to your computer and use it in GitHub Desktop.
UIButton with UIButton.Configuration
import UIKit
final class MyButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
var configuration = UIButton.Configuration.filled()
configuration.title = "My Button"
configuration.baseForegroundColor = .black
configuration.baseBackgroundColor = .red
self.configuration = configuration
configurationUpdateHandler = { button in
switch button.state {
case .normal:
button.configuration?.baseBackgroundColor = .red
case .highlighted:
button.configuration?.baseBackgroundColor = .blue
default:
break
}
}
layer.cornerRadius = 5
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment