├── .swiftpm
└── xcode
│ ├── package.xcworkspace
│ ├── contents.xcworkspacedata
│ └── xcuserdata
│ │ └── bbielik.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ └── bbielik.xcuserdatad
│ └── xcschemes
│ └── xcschememanagement.plist
├── LICENSE
├── Package.swift
├── README.md
├── Sources
└── UIViewPreview
│ └── UIViewPreview.swift
└── assets
└── screenshot.png
/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.swiftpm/xcode/package.xcworkspace/xcuserdata/bbielik.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bielikb/UIViewPreview/c539cf207b76c2903260e2c38afd7df01d64ac66/.swiftpm/xcode/package.xcworkspace/xcuserdata/bbielik.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/.swiftpm/xcode/xcuserdata/bbielik.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | UseAutoLayout.xcscheme_^#shared#^_
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | This is free and unencumbered software released into the public domain.
2 |
3 | Anyone is free to copy, modify, publish, use, compile, sell, or
4 | distribute this software, either in source code form or as a compiled
5 | binary, for any purpose, commercial or non-commercial, and by any
6 | means.
7 |
8 | In jurisdictions that recognize copyright laws, the author or authors
9 | of this software dedicate any and all copyright interest in the
10 | software to the public domain. We make this dedication for the benefit
11 | of the public at large and to the detriment of our heirs and
12 | successors. We intend this dedication to be an overt act of
13 | relinquishment in perpetuity of all present and future rights to this
14 | software under copyright law.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | OTHER DEALINGS IN THE SOFTWARE.
23 |
24 | For more information, please refer to
25 |
--------------------------------------------------------------------------------
/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version:5.1
2 | // The swift-tools-version declares the minimum version of Swift required to build this package.
3 |
4 | import PackageDescription
5 |
6 | let package = Package(
7 | name: "UIViewPreview",
8 | products: [
9 | // Products define the executables and libraries produced by a package, and make them visible to other packages.
10 | .library(
11 | name: "UIViewPreview",
12 | targets: ["UIViewPreview"]),
13 | ],
14 | targets: [
15 | .target(
16 | name: "UIViewPreview",
17 | dependencies: [])
18 | ]
19 | )
20 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | _Mattt's_ beautiful gist to gain SwiftUI previews for your UIViews turned into Swift Package.
10 |
11 | # UIViewPreview Swift Package
12 |
13 | Swift Package contains:
14 | * `UIViewPreview`
15 | * `UIViewControllerPreview`
16 |
17 |
18 | Please read more about the use-cases for `UIViewPreview` in the `NSHipster` blogpost:
19 | [https://nshipster.com/swiftui-previews/](https://nshipster.com/swiftui-previews/)
20 |
21 | ## Requirements:
22 | * macOS Catalina
23 | * Xcode 11.0 and above
24 | * Swift 5.1 and above
25 | * iOS 13.0 and above
26 | * tvOS 13.0 and above
27 |
28 | ## Installation
29 | ### Swift Package Manager
30 |
31 | Add
32 | `.package(url: "https://github.com/bielikb/UIViewPreview.git", from: "1.0.0")`
33 | to your `Package.swift` file's `dependencies`.
34 |
35 | If youre using Xcode 11.0 add `UIViewPreview` Swift Package to your target(s) using Xcode.
36 |
37 | ## PreviewProvider (Official Apple Docs)
38 |
39 | ```
40 | /// Produces view previews in Xcode.
41 | ///
42 | /// Xcode statically discovers types that conform to `PreviewProvider` and
43 | /// generates previews in the canvas for each provider it discovers.
44 | @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
45 | public protocol PreviewProvider : _PreviewProvider
46 | ```
47 |
48 | ## Example:
49 |
50 | ```
51 | import UIViewPreview
52 |
53 | #if canImport(SwiftUI) && DEBUG
54 | import SwiftUI
55 | @available(iOS 13.0, *)
56 | struct Label_Preview: PreviewProvider {
57 | static var previews: some View {
58 | UIViewPreview {
59 | let label = UILabel()
60 | label.frame = CGRect(origin: .zero,
61 | size: CGSize(width: 100, height: 100))
62 | label.text = "Text previewed in SwiftUI Preview"
63 | return label
64 | }
65 | }
66 | }
67 | #endif
68 | ```
69 |
70 | 
71 |
72 |
73 | ## LICENSE
74 | https://unlicense.org
75 |
--------------------------------------------------------------------------------
/Sources/UIViewPreview/UIViewPreview.swift:
--------------------------------------------------------------------------------
1 | #if canImport(SwiftUI) && DEBUG
2 | import SwiftUI
3 |
4 | public struct UIViewPreview: UIViewRepresentable {
5 | public let view: View
6 | public init(_ builder: @escaping () -> View) {
7 | view = builder()
8 | }
9 | // MARK: - UIViewRepresentable
10 | public func makeUIView(context: Context) -> UIView {
11 | return view
12 | }
13 | public func updateUIView(_ view: UIView, context: Context) {
14 | view.setContentHuggingPriority(.defaultHigh, for: .horizontal)
15 | view.setContentHuggingPriority(.defaultHigh, for: .vertical)
16 | }
17 | }
18 |
19 | public struct UIViewControllerPreview: UIViewControllerRepresentable {
20 | public let viewController: ViewController
21 |
22 | public init(_ builder: @escaping () -> ViewController) {
23 | viewController = builder()
24 | }
25 |
26 | // MARK: - UIViewControllerRepresentable
27 | public func makeUIViewController(context: Context) -> ViewController {
28 | viewController
29 | }
30 |
31 | @available(iOS 13.0, tvOS 13.0, *)
32 | @available(OSX, unavailable)
33 | @available(watchOS, unavailable)
34 | public func updateUIViewController(_ uiViewController: ViewController, context: UIViewControllerRepresentableContext>) {
35 | return
36 | }
37 | }
38 | #endif
39 |
--------------------------------------------------------------------------------
/assets/screenshot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bielikb/UIViewPreview/c539cf207b76c2903260e2c38afd7df01d64ac66/assets/screenshot.png
--------------------------------------------------------------------------------