Created
September 24, 2020 13:20
Representing both AppKit and UIKit Views inside the SwiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if os(macOS) | |
public typealias ViewRepresentable = NSViewRepresentable | |
public typealias NativeView = NSView | |
#elseif os(iOS) | |
public typealias ViewRepresentable = UIViewRepresentable | |
public typealias NativeView = UIView | |
#endif | |
public protocol ViewRepresentableHelper: ViewRepresentable { | |
associatedtype ViewType: NativeView | |
var configuration: (ViewType) -> () { get set } | |
func new() -> ViewType | |
} | |
public extension ViewRepresentableHelper { | |
func new() -> ViewType { ViewType() } | |
} | |
public extension ViewRepresentableHelper { | |
func makeView(_ context: Context) -> ViewType { | |
let view = new() | |
view.setContentHuggingPriority(.defaultHigh, for: .vertical) | |
view.setContentHuggingPriority(.defaultHigh, for: .horizontal) | |
return view | |
} | |
func updateView(_ view: ViewType, _ context: Context) { configuration(view) } | |
func makeUIView(context: Context) -> ViewType { makeView(context) } | |
func makeNSView(context: Context) -> ViewType { makeView(context) } | |
func updateUIView(_ uiView: ViewType, context: Context) { updateView(uiView, context) } | |
func updateNSView(_ nsView: ViewType, context: Context) { updateView(nsView, context) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment