Skip to content

Instantly share code, notes, and snippets.

@MojtabaHs
Last active April 18, 2023 10:33
Show Gist options
  • Save MojtabaHs/8a2b9294de396c81aea1485d38c9b372 to your computer and use it in GitHub Desktop.
Save MojtabaHs/8a2b9294de396c81aea1485d38c9b372 to your computer and use it in GitHub Desktop.
This is a simple file you can use to bring any UIKit UIView in to the SwiftUI. Fully customizable.
import SwiftUI
protocol UIViewRepresentableHelper: UIViewRepresentable {
var configuration: (UIViewType) -> () { get set }
}
@available(iOS 13.0, *)
extension UIViewRepresentableHelper {
func makeUIView(context: UIViewRepresentableContext<Self>) -> UIViewType {
let uiView = UIViewType()
uiView.setContentHuggingPriority(.defaultHigh, for: .vertical)
uiView.setContentHuggingPriority(.defaultHigh, for: .horizontal)
return uiView
}
func updateUIView(_ uiView: UIViewType, context: UIViewRepresentableContext<Self>) { configuration(uiView) }
}
// MARK: - Usage:
struct <#UIViewForSwiftUI#>: UIViewRepresentableHelper {
typealias UIViewType = <#The Original UIView Type#> /* e.g. UIVisualEffectView */
var configuration = { (view: UIViewType) in }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment