Skip to content

Instantly share code, notes, and snippets.

@Fiser12
Created January 20, 2023 12:13
Show Gist options
  • Select an option

  • Save Fiser12/55f18667bae0e7cae64462e0a8e20e4e to your computer and use it in GitHub Desktop.

Select an option

Save Fiser12/55f18667bae0e7cae64462e0a8e20e4e to your computer and use it in GitHub Desktop.
SwiftUI if else modifiers
extension View {
@ViewBuilder func `if`<Content: View>(_ condition: Bool, transform: (Self) -> Content) -> some View {
if condition {
transform(self)
} else {
self
}
}
@ViewBuilder func `if`<Content: View, ContentElse: View>(
_ condition: Bool,
transform: (Self) -> Content,
else: ((Self) -> ContentElse)
) -> some View {
if condition {
transform(self)
} else {
`else`(self)
}
}
}
// var body: some View {
// inputField
// .if(compact) { compactView in
// compactView
// .font(.title3)
// .padding(.horizontal)
// } else: { extendedView in
// extendedView
// .frame(height: 35)
// .padding(5)
// }
// }
@Fiser12
Copy link
Author

Fiser12 commented Jan 20, 2023

I created this view extension to make conditional modifiers on swiftui

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment