Skip to content

Instantly share code, notes, and snippets.

@karigrooms
Last active February 19, 2021 18:19
Show Gist options
  • Save karigrooms/02a434001f8248d6426d2f93c12c9d95 to your computer and use it in GitHub Desktop.
Save karigrooms/02a434001f8248d6426d2f93c12c9d95 to your computer and use it in GitHub Desktop.
SwiftUI composition order of view modifiers for Lessons in SwiftUI blog post (first example)
import SwiftUI
struct MyButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.fixedSize()
// apply background color BEFORE size modifiers
.background(Color.blue)
.foregroundColor(Color.white)
// apply size modifiers
.frame(width: nil, height: 48)
.padding(.horizontal, 24)
// apply corner radius after everything
.cornerRadius(24)
}
}
struct MyButtonStyle_Previews: PreviewProvider {
static var previews: some View {
Button("Button", action: {})
.buttonStyle(MyButtonStyle())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment