Created
August 19, 2020 16:59
-
-
Save larsaugustin/a61873eec70ac35367c54d5ee5dd3d61 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
For those times you need an
else
as well: