Skip to content

Instantly share code, notes, and snippets.

@stleamist
Last active October 3, 2020 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stleamist/2e8a3399c039b18c0a11821aeb3a6d85 to your computer and use it in GitHub Desktop.
Save stleamist/2e8a3399c039b18c0a11821aeb3a6d85 to your computer and use it in GitHub Desktop.
import SwiftUI
extension View {
@ViewBuilder
func modify<Modified: View>(@ModifiedViewBuilder modificationBlock: (Self) -> Modified) -> some View {
let modified = modificationBlock(self)
if modified is EmptyView {
self
} else {
modified
}
}
}
@_functionBuilder
struct ModifiedViewBuilder {
static func buildBlock() -> EmptyView {
EmptyView()
}
static func buildBlock<Content: View>(_ content: Content) -> Content {
content
}
}
import SwiftUI
struct ContentView: View {
@State private var text = ""
var body: some View {
TextField("", text: $text)
.modify {
#if os(iOS)
$0
.autocapitalization(.none)
#endif
}
.foregroundColor(.blue)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment