├── .gitignore ├── Package.swift ├── README.md ├── Sources └── UIViewCanvas │ └── UIViewCanvas.swift └── docs └── images └── xcode-screenshot1.png /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/ -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.5 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: "UIViewCanvas", 8 | platforms: [ 9 | .iOS(.v13) 10 | ], 11 | products: [ 12 | .library( 13 | name: "UIViewCanvas", 14 | targets: ["UIViewCanvas"]), 15 | ], 16 | dependencies: [ 17 | ], 18 | targets: [ 19 | .target( 20 | name: "UIViewCanvas", 21 | dependencies: []), 22 | ] 23 | ) 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UIViewCanvas 2 | 3 | This package allows you to quickly setup Xcode canvas to any UIView/ViewController. 4 | 5 | 6 | 7 | ## Why? 8 | 9 | Run emulator everytime you need to test a small change in your View is painfull. 10 | 11 | ## Requirements 12 | 13 | ✓ Xcode 11+
14 | ✓ Swift 4.2+
15 | ✓ iOS 13+ project
16 | 17 | ## How to use? 18 | 19 | ### Step 1 20 | Select your project on Xcode and go to `Files` > `Add Packages` and paste this repository URL `https://github.com/outrowender/UIViewCanvas` on search field. 21 | 22 | Select a version and click `Install`. 23 | 24 | ### Step 2 25 | Add the previewer to the end of your view file: 26 | 27 | ```swift 28 | import SwiftUI 29 | import UIViewCanvas 30 | 31 | struct MyPreview: PreviewProvider { 32 | static var previews: some View { 33 | ViewCanvas(for: MyCustomUIView()) 34 | } 35 | } 36 | ``` 37 | 38 | You can instantiate a entire ViewController as well, using `ViewControllerCanvas`: 39 | 40 | ```swift 41 | struct MyPreview: PreviewProvider { 42 | static var previews: some View { 43 | ViewControllerCanvas(for: MyViewController()) 44 | } 45 | } 46 | ``` 47 | 48 | If you want to customize your preview, check `Group` component and use a `.previewLayout` to set a custom canvas to your preview. 49 | 50 | ```swift 51 | struct MyPreview: PreviewProvider { 52 | static var previews: some View { 53 | Group{ 54 | ViewCanvas(for: MyCustomUIView()) 55 | } 56 | .previewLayout(.fixed(width: 350, height: 350)) 57 | } 58 | } 59 | ``` 60 | 61 | ### Step 3 62 | You may need to reopen your `.swift` file in Xcode. 63 | 64 | You can use `⌥ + ⌘ + P` to quickly resume your canvas. 65 | 66 | ## Credits 67 | 68 | This package was a insight that I got from a @bhlvoong video. 69 | -------------------------------------------------------------------------------- /Sources/UIViewCanvas/UIViewCanvas.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UIKitCanvas.swift 3 | // UIViewCanvas 4 | // 5 | // Created by Wender Patrick on 08/03/22. 6 | 7 | import UIKit 8 | 9 | #if canImport(SwiftUI) 10 | import SwiftUI 11 | 12 | public struct ViewCanvas: UIViewRepresentable { 13 | private let view: UIView 14 | 15 | public init(for view: UIView){ 16 | self.view = view 17 | } 18 | 19 | public func makeUIView(context: Context) -> UIView { 20 | return view 21 | } 22 | 23 | public func updateUIView(_ view: UIView, context: Context) { 24 | } 25 | } 26 | 27 | public struct ViewControllerCanvas: UIViewControllerRepresentable { 28 | private let view: UIViewController 29 | 30 | public init(for view: UIViewController){ 31 | self.view = view 32 | } 33 | 34 | public func makeUIViewController(context: Context) -> some UIViewController { 35 | return view 36 | } 37 | 38 | public func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { 39 | } 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /docs/images/xcode-screenshot1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/outrowender/UIViewCanvas/212e9fceb25aa6ee07872462968022180db95cce/docs/images/xcode-screenshot1.png --------------------------------------------------------------------------------