Code Monkey home page Code Monkey logo

uiviewpreviewprovider's Introduction

UIViewPreviewProvider

This is a helper class for displaying UIViews inside the Xcode preview canvas meant to display SwiftUI.
It allows you to quickly iterate on your UIViews without having to rebuild your entire app.
This is especially useful if your project needs to supports iOS < 13 and you can't start using SwiftUI just yet.

Adding UIViewPreviewProvider to your project.

Just copy UIViewPreviewProvider.swift into your project. I don't think this is large enough to warrant adding a whole library to your project.

Rendering UIView previews in your project

In the file where your view is defined:

// This if check is important if you're working with a project that supports iOS < 13
// If you're working with an iOS 13+ project then you can omit the canImport(SwiftUI) check
#if canImport(SwiftUI) && DEBUG

import SwiftUI

@available(iOS 13.0, *)
struct MyView_Preview: PreviewProvider, UIViewPreviewProvider {
    
    // Provide previews of your UIView
    static let uiPreviews: [Preview] = {

        // Create your views and populate them with some dummy data
        
        let view1 = MyView()
        view1.someProperty = "foo"
        let view1Preview = Preview(view1, displayName: "View 1")

        let view2 = MyView()
        view2.someProperty = "bar"
        let view2Preview = Preview(view2, displayName: "View 2")

        return [view1Preview, view2Preview]
    }()
}

#endif

Open up the preview Xcode canvas by pressing (โŒฅ + โŒ˜ + return) and your previews should show up.

You may need to hit the Resume button for them to show up

It should look something like this: screenshot

Examples

See Example.swift for more info.

If you're using MVVM see MVVM Example.swift to see how you can generate previews directly from your ViewModels.

uiviewpreviewprovider's People

Contributors

mrabiciu avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.