Giter Club home page Giter Club logo

previewview's Introduction

Tip

The #Preview macro (WWDC23 session) has replaced this library if you are using Xcode 15 or higher.

If your Minimum Deployment target is set earlier than iOS 17 you will need to add an availability check.

@available(iOS 17, *)
#Preview {
  YourViewController()
}

@available(iOS 17, *)
#Preview {
  YourView()
}

PreviewView

Make use of SwiftUI previews for rapidly prototyping your UIViewControllers and UIViews!

The SwiftUI preview canvas is tied to a specific version of Xcode, not the the target OS version. This means you can make use of this utility even if you're not targeting iOS 13 or higher, as long as you're using Xcode 10 or higher.

My iOS deployment target is below iOS 13

If you're targeting an iOS version earlier than iOS 13 then you may be get an error such as:

Compiling failed: '__designTimeString(_:fallback:)' is only available in iOS 13.0 or newer

Other known variants may be:

  • __designTimeInteger(_:fallback:)
  • __designTimeBoolean(_:fallback:)
  • __designTimeFloat(_:fallback:)

To solve this issue this library provides another target, PreviewViewLegacyOSCompileFix, which adds functions annotated with @backDeployed(before:) so these functions exist on versions earlier than iOS 13.

In addition the normal import PreviewView and import SwiftUI imports alongside your PreviewProvider you will need to add import PreviewViewLegacyOSCompileFix to ensure the back deployed functions are present.

Don't forget to mark your PreviewProvider with @available(iOS 13, *).

Installation

You can manually drop the files into your project, or take a look at Apple's documentation for adding Swift Packages in Xcode.

Adding this as a dependency on a Swift Package is not recommended as it will then force the dependency on anyone that consumes your library.

Previewing a view

struct YourViewController_Previews: PreviewProvider {
    static var previews: some View {
        ViewPreview(YourView())
            .previewLayout(.fixed(width: 375, height: 86))
    }
}

Important: Update the previewLayout values to be the typical size of your view.

Previewing a view controller

Standalone

struct YourViewController_Previews: PreviewProvider {
    static var previews: some View {
        ViewControllerPreview(YourViewController())
            .edgesIgnoringSafeArea(.all)
    }
}

If you wish to test a custom UINavigationController you can do so with ViewControllerPreview.

struct YourNavigationController_Previews: PreviewProvider {
    static var previews: some View {
        ViewControllerPreview(YourNavigationController())
            .edgesIgnoringSafeArea(.all)
    }
}

Embedded in a UINavigationController

struct YourViewController_Previews: PreviewProvider {
    static var previews: some View {
        NavigationControllerPreview {
            YourViewController()
        }
        .edgesIgnoringSafeArea(.all)
    }
}

The body content of the NavigationControllerPreview accepts an entire navigation stack, allowing your previews to show back bar button items, and even be navigatable in Live Preview.

struct DetailViewController_Previews: PreviewProvider {
    static var previews: some View {
        NavigationControllerPreview {
            ListViewController()
            DetailViewController()
        }
        .edgesIgnoringSafeArea(.all)
    }
}

You can customise the navigation bar settings and toolbar settings of the navigation controller via the initializer parameters.

struct DetailViewController_Previews: PreviewProvider {
    static var previews: some View {
        NavigationControllerPreview(barStyle: .largeTitle, showsToolbar: true) {
            ListViewController()
            DetailViewController()
        }
        .edgesIgnoringSafeArea(.all)
    }
}

Embedded in a UITabBarController

struct YourViewController_Previews: PreviewProvider {
    static var previews: some View {
        TabBarControllerPreview {
            ViewControllerPreview(YourViewController())
        }
        .edgesIgnoringSafeArea(.all)
    }
}

Displaying a single tab would be weird, so to allow your previews to closely match your real app you can provide the other tabs within the body.

struct YourViewController_Previews: PreviewProvider {
    static var previews: some View {
        TabBarControllerPreview {
            PreviewBlankTabItem(title: "First", image: UIImage(systemName: "capsule"))
            
            ViewControllerPreview(YourViewController())
            
            ViewControllerPreview(YourOtherViewController())
        }
        .edgesIgnoringSafeArea(.all)
    }
}

You can even embed your view controllers in a navigation controller to get the full in-app experience.

struct YourViewController_Previews: PreviewProvider {
    static var previews: some View {
        TabBarControllerPreview {
            PreviewBlankTabItem(title: "First", image: UIImage(systemName: "capsule"))
            
            NavigationControllerPreview {
                YourViewController()
            }
            
            PreviewBlankTabItem(title: "Third", image: UIImage(systemName: "diamond"))
        }
        .edgesIgnoringSafeArea(.all)
    }
}

previewview's People

Contributors

theoriginalbit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

previewview's Issues

Doesn't work in Xcode 14 when project targets iOS 12

This may be more of an issue/lockdown of the Previews starting with Xcode 14.

When trying to use the same preview code that works with Xcode 13.4.1, the Preview gives a compile error:

Compiling failed: '__designTimeString(_:fallback:)' is only available in iOS 13.0 or newer

Here is my preview code:

// MARK: - Preview
#if canImport(SwiftUI) && DEBUG
import SwiftUI

@available(iOS 13, *)
struct ContentItemHeaderView_Previews: PreviewProvider {
    static let headerView: ContentItemHeaderView = {
        let view = ContentItemHeaderView()
        view.imageView.image = UIImage(named: "first-launch-background4")
        view.titleText = "Connect With Mother Nature When You Really Want To"
        view.publisherText = "Apple.com"
        view.authorNameText = "Jonny Appleseed"

        return view
    }()

    static var previews: some View {
        Group {
            ViewPreview(headerView)
                .previewLayout(.fixed(width: 390, height: 95))
            ViewPreview(headerView)
                .preferredColorScheme(.dark)
                .previewLayout(.fixed(width: 390, height: 95))
        }
    }
}
#endif

Doesn't work in large app

SwiftUIPreview render exceed 30 seconds and throw error when preview a custom view inside a large app (take over 10 mins to compile)

Readme needs update on example section

PreviewView as demonstrated in Examples doesnt seem to work with latest HEAD.

Expected

Examples on Readme should be Preview

struct YourViewController_Previews: PreviewProvider {
    static var previews: some View {
        Preview(for: YourViewController())
            .edgesIgnoringSafeArea(.all)
            .previewDevice(PreviewDevice(rawValue: "iPhone 11"))
    }
}

Actual

Readme uses PreviewView in examples

struct YourViewController_Previews: PreviewProvider {
    static var previews: some View {
        Preview(for: YourViewController())
            .edgesIgnoringSafeArea(.all)
            .previewDevice(PreviewDevice(rawValue: "iPhone 11"))
    }
}

Does not build for x86_64 simulator for porject which have to use x86_64 architecture for Simulator on Apple Silicone devices

Could not find module 'PreviewView' for target 'x86_64-apple-ios-simulator'; found: arm64-apple-ios-simulator

SPM builds for arm64 despite the project explicitly specifying arm64 as an excluded architecture for the Simulator.
Due to this, a project that depends on older libraries that have no simulator[arm64] support fails to link with this library.

A CocoaPods support would have solved this issue due to Cocoapods being able to support architecture exclusion

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.