11import SwiftUI
2- import SwiftUIIntrospect
2+ @ _spi ( Internals ) import SwiftUIIntrospect
33
44struct UIViewRepresentableShowcase : View {
55 var body : some View {
66 VStack ( spacing: 10 ) {
7- Text ( " .introspect(.view, ...) " )
8- . lineLimit ( 1 )
9- . minimumScaleFactor ( 0.5 )
10- . font ( . system( . subheadline, design: . monospaced) )
7+ GenericViewRepresentable ( )
118 #if os(iOS) || os(tvOS) || os(visionOS)
129 . introspect(
1310 . view,
@@ -20,35 +17,6 @@ struct UIViewRepresentableShowcase: View {
2017 view. layer? . backgroundColor = NSColor . cyan. cgColor
2118 }
2219 #endif
23-
24- Button ( " A button " , action: { } )
25- . padding ( 5 )
26- #if os(iOS) || os(tvOS) || os(visionOS)
27- . introspect(
28- . view,
29- on: . iOS( . v13, . v14, . v15, . v16, . v17, . v18, . v26) , . tvOS( . v13, . v14, . v15, . v16, . v17, . v18, . v26) , . visionOS( . v1, . v2, . v26)
30- ) { view in
31- view. backgroundColor = . yellow
32- }
33- #elseif os(macOS)
34- . introspect( . view, on: . macOS( . v10_15, . v11, . v12, . v13, . v14, . v15, . v26) ) { view in
35- view. layer? . backgroundColor = NSColor . yellow. cgColor
36- }
37- #endif
38-
39- Image ( systemName: " scribble " )
40- #if os(iOS) || os(tvOS) || os(visionOS)
41- . introspect(
42- . view,
43- on: . iOS( . v13, . v14, . v15, . v16, . v17, . v18, . v26) , . tvOS( . v13, . v14, . v15, . v16, . v17, . v18, . v26) , . visionOS( . v1, . v2, . v26)
44- ) { view in
45- view. backgroundColor = . blue
46- }
47- #elseif os(macOS)
48- . introspect( . view, on: . macOS( . v10_15, . v11, . v12, . v13, . v14, . v15, . v26) ) { view in
49- view. layer? . backgroundColor = NSColor . blue. cgColor
50- }
51- #endif
5220 }
5321 . padding ( )
5422 #if os(iOS) || os(tvOS) || os(visionOS)
@@ -65,3 +33,35 @@ struct UIViewRepresentableShowcase: View {
6533 #endif
6634 }
6735}
36+
37+ @MainActor
38+ struct GenericViewRepresentable : PlatformViewControllerRepresentable {
39+ #if canImport(UIKit)
40+ typealias UIViewControllerType = PlatformViewController
41+ #elseif canImport(AppKit)
42+ typealias NSViewControllerType = PlatformViewController
43+ #endif
44+
45+ func makePlatformViewController( context: Context ) -> PlatformViewController {
46+ let controller = PlatformViewController ( nibName: nil , bundle: nil )
47+ controller. view. translatesAutoresizingMaskIntoConstraints = false
48+
49+ let widthConstraint = controller. view. widthAnchor. constraint ( greaterThanOrEqualToConstant: . greatestFiniteMagnitude)
50+ widthConstraint. priority = . defaultLow
51+
52+ let heightConstraint = controller. view. heightAnchor. constraint ( greaterThanOrEqualToConstant: . greatestFiniteMagnitude)
53+ heightConstraint. priority = . defaultLow
54+
55+ NSLayoutConstraint . activate ( [ widthConstraint, heightConstraint] )
56+
57+ return controller
58+ }
59+
60+ func updatePlatformViewController( _ controller: PlatformViewController , context: Context ) {
61+ // NO-OP
62+ }
63+
64+ static func dismantlePlatformViewController( _ controller: PlatformViewController , coordinator: Coordinator ) {
65+ // NO-OP
66+ }
67+ }
0 commit comments