-
-
Save wb-towa/b8217c887a34d49bb691908216dbe2f8 to your computer and use it in GitHub Desktop.
Conditional view modifiers in SwiftUI
This file contains 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
struct ExampleView: View { | |
var body: some View { | |
Text("Example") | |
.if(true) { $0.foregroundColor(.red) } | |
} | |
} |
This file contains 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 | |
public func `if`<V>(_ condition: Bool, input: (Self) -> V) -> some View where V: View { | |
if condition { | |
input(self) | |
} else { | |
self | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment