Last active
December 3, 2024 07:42
-
Star
(130)
You must be signed in to star a gist -
Fork
(11)
You must be signed in to fork a gist
-
-
Save mattt/ff6b58af8576c798485b449269d43607 to your computer and use it in GitHub Desktop.
Generic structures to host previews of UIView and UIViewController subclasses.
This file contains hidden or 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 UIKit | |
#if canImport(SwiftUI) && DEBUG | |
import SwiftUI | |
struct UIViewControllerPreview<ViewController: UIViewController>: UIViewControllerRepresentable { | |
let viewController: ViewController | |
init(_ builder: @escaping () -> ViewController) { | |
viewController = builder() | |
} | |
// MARK: - UIViewControllerRepresentable | |
func makeUIViewController(context: Context) -> ViewController { | |
viewController | |
} | |
func updateUIViewController(_ uiViewController: ViewController, context: UIViewControllerRepresentableContext<UIViewControllerPreview<ViewController>>) { | |
return | |
} | |
} | |
#endif |
This file contains hidden or 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 UIKit | |
#if canImport(SwiftUI) && DEBUG | |
import SwiftUI | |
struct UIViewPreview<View: UIView>: UIViewRepresentable { | |
let view: View | |
init(_ builder: @escaping () -> View) { | |
view = builder() | |
} | |
// MARK: - UIViewRepresentable | |
func makeUIView(context: Context) -> UIView { | |
return view | |
} | |
func updateUIView(_ view: UIView, context: Context) { | |
view.setContentHuggingPriority(.defaultHigh, for: .horizontal) | |
view.setContentHuggingPriority(.defaultHigh, for: .vertical) | |
} | |
} | |
#endif |
I turned your solution into Swift Package here:
https://github.com/bielikb/UIViewPreview
If there's anything missing, please let me know ;)
@bielikb Cool, thanks for sharing. Only real correction I'd offer is that my name is Mattt, not Matt Thompson.
Thanks, ill fix it right after the lunch 🙂
EDIT:
done
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@bielikb In this case, I think that copy-pasting offers the best form of code reuse, so I don't have any plans to release this as a Swift Package, CocoaPod, or the like. With that said, the license under which this code is licensed permits anyone to publish and distribute this code however they want.