Swiftui 3 and iOS15 gave us the possibility to use styling more easily, so instead of
.pickerStyle(WheelPickerStyle())
we might use
.pickerStyle(.wheel)
At the same time, we can determine custom styles, for example:
struct CustomButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.foregroundColor(Color.blue)
.background(RoundedRectangle(cornerRadius: 12).fill(Color.red))
.scaleEffect(configuration.isPressed ? 0.9 : 1.0)
.frame(width: 40, height: 40)
}
}
However, to use the above custom style, I have to write
.buttonStyle(CustomButtonStyle())
How might I achieve the possibility to use the custom style with a simple modifier like this?
.buttonStyle(.custom)
We can create extension to ButtonStyle
, like
extension ButtonStyle where Self == CustomButtonStyle {
static var custom: CustomButtonStyle { CustomButtonStyle() }
}
and then use it as
.buttonStyle(.custom)
Tested with Xcode 13 / iOS15
Note: this approach does not work with Xcode 12
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments