Skip to content

Instantly share code, notes, and snippets.

@vincefried
Created September 8, 2023 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vincefried/5c08b308ddafc91193d22fe65fc0140d to your computer and use it in GitHub Desktop.
Save vincefried/5c08b308ddafc91193d22fe65fc0140d to your computer and use it in GitHub Desktop.
A small demonstration of how I would like a way to modify a wrapped UIView.
import SwiftUI
struct ContentView2: View {
var body: some View {
VStack {
SimpleRepresentable(text: "Test")
SimpleRepresentableWithPadding(text: "Test")
}
}
}
// I would like to avoid something like this.
struct SimpleRepresentableWithPadding: View {
let text: String
var body: some View {
SimpleRepresentable(text: text)
.padding()
}
}
struct SimpleRepresentable: UIViewRepresentable {
let text: String
func makeUIView(context: Context) -> UILabel {
let label = UILabel()
label.text = text
return label
}
func updateUIView(_ uiView: UILabel, context: Context) {
uiView.text = text
}
// There should be something like this where I can directly modify the content in a way as part of this representable.
// func body(content: Content) -> some View {
// content
// .padding()
// }
}
#Preview {
ContentView2()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment