Enables SwiftUI live previews for UIKit views and view controllers.
- macOS Catalina or later
- Xcode 12 or later
- iOS Deployment Target 12.0 or later
In Xcode 13 or later, select File > Add Packages...
In Xcode 12, select File > Swift Packages > Add Package Dependency...
Add https://github.com/nicoelayda/UIKitLivePreview.git
as the package repository URL.
or
If you have an existing Package.swift
file, add UIKitLivePreview
package to your target's dependencies.
dependencies: [
.package(url: "https://github.com/nicoelayda/UIKitLivePreview.git", .upToNextMajor(from: "1.3.1"))
]
-
Add
UIKitLivePreview
to yourCartfile
.github "nicoelayda/UIKitLivePreview" ~> 1.3.1
-
Run
carthage update --use-xcframeworks
-
Drag
UIKitLivePreview.xcframework
inCarthage/Build
into your application target's Frameworks, Libraries and Embedded Content.
-
Add
UIKitLivePreview
to yourPodfile
.pod 'UIKitLivePreview', '~> 1.3.1'
-
Run
pod install
Copy the contents of Sources/UIKitLivePreview
to your project.
A prebuilt XCFramework binary is also available
- Import
UIKitLivePreview
in your view or view controller. - In the same Swift file, define a new struct conforming to
PreviewProvider
. - Inside the
previews
property:- Initialise your UIKit view or view controller.
- Call
preview()
on it to create a wrapped SwiftUIView
instance. - Return the preview instance.
- Optionally, you may chain
ViewModifier
s to customise the preview. See example below.
final class MyViewController: UIViewController { /* ... */ }
#if DEBUG && canImport(SwiftUI)
import SwiftUI
@available(iOS 13.0, *)
struct MyViewController_Preview: PreviewProvider {
static var previews: some View {
MyViewController()
.preview()
.device(.iPhone11)
.landscape()
}
}
#endif
NOTE: If your project is targeting iOS 12, it is recommended to wrap the PreviewProvider
struct in a #if canImport(SwiftUI)
directive and add the @available(iOS 13.0, *)
attribute to it.
Check out UIKitLivePreview-Examples for a sample project.
MIT. See LICENSE.