Skip to content

Instantly share code, notes, and snippets.

@wb-towa
Forked from larsaugustin/ExampleView.swift
Created August 19, 2020 17:19
Show Gist options
  • Save wb-towa/b8217c887a34d49bb691908216dbe2f8 to your computer and use it in GitHub Desktop.
Save wb-towa/b8217c887a34d49bb691908216dbe2f8 to your computer and use it in GitHub Desktop.
Conditional view modifiers in SwiftUI
struct ExampleView: View {
var body: some View {
Text("Example")
.if(true) { $0.foregroundColor(.red) }
}
}
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