Created
January 20, 2023 12:13
-
-
Save Fiser12/55f18667bae0e7cae64462e0a8e20e4e to your computer and use it in GitHub Desktop.
SwiftUI if else modifiers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| // } | |
| // } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I created this view extension to make conditional modifiers on swiftui