Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Forked from vincefried/SimpleRepresentable.swift
Last active September 8, 2023 07:12
Show Gist options
  • Save chriseidhof/cac0515afe0ba0c561f85f6c0f12786b to your computer and use it in GitHub Desktop.
Save chriseidhof/cac0515afe0ba0c561f85f6c0f12786b 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 UIKit
import SwiftUI
struct ContentView2: View {
var body: some View {
VStack {
SimpleRepresentable(text: "Test")
}
}
}
struct SimpleRepresentable: View {
var text: String
var minimumScaleFactor: Double = 0.1
var backgroundColor: Color = .blue
var body: some View {
_SimpleRepresentable(parent: self)
.padding()
.background(backgroundColor)
}
}
fileprivate struct _SimpleRepresentable: UIViewRepresentable {
let parent: SimpleRepresentable
func makeUIView(context: Context) -> UILabel {
let label = UILabel()
label.text = parent.text
label.minimumScaleFactor = parent.minimumScaleFactor
return label
}
func updateUIView(_ uiView: UILabel, context: Context) {
uiView.text = parent.text
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment