View Styles
- Certain views can be changed by applying a style
- Automatic based on context unless specified
- Applied through the environment (i.e., to all subviews)
- Can create custom styles if style protocol defines
makeBody
Discussion
- When to use a style vs. just a custom view?
Additional Style Properties
If you also need to pass arguments to your style, you can add them like any other struct and pass the arguments through the extension. Here is an example using a style conforming to ButtonStyle
Add the property to the struct.
1struct MyButtonStyle: ButtonStyle {
2 let myToggleProperty: Bool
3
4 func makeBody(configuration: Configuration) -> some View {
5 //
6 }
7}
Change the var
to a func
with a parameter that is passed to the struct.
1extension ButtonStyle where Self == MyButtonStyle {
2 static func myButton(myToggle: Bool): Self {
3 .init(myToggleProperty: myToggle)
4 }
5}
Pass your value as an argument where you use the button style.
1Button("My Button Style") {
2 // no-op
3}
4.buttonStyle(.myButton(myToggle: true))
Environment Variables
You will likely want to change the appearance and interactivity of your styles when a view has been set as disabled. This is done with the .disabled
view modifier and can be retrieved with the @Environment(\.isEnabled)
. Why are the inverse of each other? I do not know.
The following environment variables don't seem widely used but could be interesting to consider in your custom styles.
1\.controlSize
2\.labelsVisibility
3\.toolbarLabelStyle
Other Customizable Styles
https://developer.apple.com/documentation/swiftui/datepickerstyle https://developer.apple.com/documentation/swiftui/menustyle https://developer.apple.com/documentation/swiftui/gaugestyle https://developer.apple.com/documentation/swiftui/progressviewstyle https://developer.apple.com/documentation/swiftui/texteditorstyle https://developer.apple.com/documentation/swiftui/tablestyle https://developer.apple.com/documentation/swiftui/disclosuregroupstyle https://developer.apple.com/documentation/swiftui/navigationsplitviewstyle https://developer.apple.com/documentation/swiftui/controlgroupstyle https://developer.apple.com/documentation/swiftui/formstyle https://developer.apple.com/documentation/swiftui/groupboxstyle https://developer.apple.com/documentation/swiftui/labeledcontentstyle
Style Not Customizable
https://developer.apple.com/documentation/swiftui/pickerstyle https://developer.apple.com/documentation/swiftui/textfieldstyle https://developer.apple.com/documentation/swiftui/liststyle https://developer.apple.com/documentation/swiftui/tabviewstyle https://developer.apple.com/documentation/swiftui/indexviewstyle