Last active
April 18, 2023 10:33
-
-
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.
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
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