Skip to content

Instantly share code, notes, and snippets.

@mattt
Last active January 8, 2024 23:09
Show Gist options
  • Save mattt/ff6b58af8576c798485b449269d43607 to your computer and use it in GitHub Desktop.
Save mattt/ff6b58af8576c798485b449269d43607 to your computer and use it in GitHub Desktop.
Generic structures to host previews of UIView and UIViewController subclasses.
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
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
@mattt
Copy link
Author

mattt commented Oct 14, 2019

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to https://unlicense.org

@lionhylra
Copy link

Is there any reason why UIViewControllerPreview and UIViewPreview takes generics? It looks it's fine to just use UIView and UIViewController for property type and closure return type.
And it seems @escaping is not necessary.

@bielikb
Copy link

bielikb commented Feb 6, 2020

Hey, this is perfect!
Could we turn these two preview helpers into Swift Package so it would be easier to share them?

Thanks

@mattt
Copy link
Author

mattt commented Feb 6, 2020

@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.

@bielikb
Copy link

bielikb commented Feb 7, 2020

I turned your solution into Swift Package here:
https://github.com/bielikb/UIViewPreview

If there's anything missing, please let me know ;)

@mattt
Copy link
Author

mattt commented Feb 7, 2020

@bielikb Cool, thanks for sharing. Only real correction I'd offer is that my name is Mattt, not Matt Thompson.

@bielikb
Copy link

bielikb commented Feb 7, 2020

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