Tip
Did you know Apple introduced UIButton.Configuration
in iOS 15?
Using the new Configuration API you can create a button like this library achieves. Simply do the following:
var initialConfig: UIButton.Configuration = .filled()
initialConfig.title = "Click me!"
initialConfig.buttonSize = .medium
// ...
let button = UIButton(configuration: initialConfig)
// ...
button.configurationUpdateHandler = { button in
var currentConfig = button.configuration
if button.isEnabled {
currentConfig?.baseBackgroundColor = .systemBlue
} else {
currentConfig?.background.backgroundColor = .systemGray
}
// ...
}
A very simple library that provides a UIButton
implementation that allows you to create a button that looks like the Apple iOS Sketch library's 'Shape (Custom)' button.
The 'Shape (Custom)' button looks like:
The API is the same as with setting anything else that is state aware. The below code will produce a button like the image above.
import ShapeButton
// …
let myButton = ShapeButton(continuousCornerRadius: ShapeButton.cornerRadiusUseStandard)
myButton.setBackgroundColor(.systemBlue, for: .normal)
myButton.setTitleColor(.white, for: .normal)
// Stateful colors
myButton.setBackgroundColor(.systemGray, for: .disabled)
myButton.setTitleColor(.systemGray4, for: .disabled)
NOTE: Internally setBackgroundColor
will generate a UIImage
and call through to setBackgroundImage(_:for:)
overriding any previous background image that may have been set.
Swift Package Manager