Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Docs/Preview.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Current Status

OpenSwiftUI supports Xcode Preview rendering through the shim introduced in [PR #829](https://github.com/OpenSwiftUIProject/OpenSwiftUI/pull/829). Source builds and binary distributions must also satisfy the linkage requirements below.
OpenSwiftUI supports Xcode Preview rendering through the shim introduced in [PR #829](https://github.com/OpenSwiftUIProject/OpenSwiftUI/pull/829). macOS Preview support was enabled by the binary framework fixes in [PR #952](https://github.com/OpenSwiftUIProject/OpenSwiftUI/pull/952). Source builds and binary distributions must also satisfy the linkage requirements below.

### How It Works

Expand All @@ -17,6 +17,8 @@ Since OpenSwiftUI cannot use SwiftUI's `#Preview` macro directly (it targets `Sw
}
```

The same preview declaration works on both supported targets. Native macOS targets return an `NSViewController` backed by `NSHostingController`; iOS and Mac Catalyst targets return a `UIViewController` backed by `UIHostingController`.

### Binary XCFramework Requirements

Xcode Preview's XOJIT loading model requires both `OpenSwiftUI.framework` and `OpenSwiftUICore.framework` to be dynamic. Packaging `OpenSwiftUICore` as a static framework allows Preview to relink another copy of Core into the preview product, which can duplicate runtime metadata and crash the Preview process.
Expand Down
11 changes: 7 additions & 4 deletions Example/Shared/PreviewShims.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ extension SwiftUI.View {
}
#endif

#if canImport(UIKit)
// FIXME: Not working on macOS yet

#Preview("HostingVC") {
ContentView()
._previewVC()
}

#Preview("CAHostingLayerExample") {
#if canImport(AppKit) && !targetEnvironment(macCatalyst)
CAHostingLayerExample(
content: ContentView(),
size: CGSize(width: 500, height: 300)
).makeViewController()
#else
CAHostingLayerExample(
content: ContentView(),
size: UIScreen.main.bounds.size
).makeViewController()
#endif
}
#endif
Loading