66 questions
0
votes
0
answers
48
views
How do I create a Preview for a Scene?
The top level of my app looks like:
struct MyApp: App {
var body: some Scene {
DocumentGroup(newDocument: MyDocument()) { file in
MyContentView(document: file.$document)
...
1
vote
1
answer
89
views
SwiftUI Preview crashes due to array length
In the following code, the Xcode Preview crashes when the array is more than 9 elements but does not crash with 9 or less elements.
CompileDylibError: Failed to build ContentView.swift
Compiling ...
0
votes
1
answer
94
views
SwiftUI Preview not loading Color assets from .xcassets in framework target
I'm building a modular iOS app with SwiftUI views in a framework target (MyFrameworkTarget). I added a new .xcassets catalog to this target and defined some named colors like "durationBackground&...
0
votes
2
answers
103
views
How do you pass an @ObservedObject var as a parameter into a view struct with an init() within a Preview View?
I have created an app that lists recipes on startup. The app fetches data from a URL and downloads static data. When I scroll down the list, it downloads and caches the images using CacheManager. Once ...
-3
votes
1
answer
481
views
I am not able to use SwiftUI previews
Xcode Errors:
"Build failed because DotLottie.swiftmodule is not built for arm64. Would you like to show all run destinations instead?"
"Ensure all targets are configured to build for ...
2
votes
3
answers
842
views
How to mock view model for SwiftUI previews?
My SwiftUI app consists of different views with their own view models which handle data fetching etc. This works fine in the simulator, but I'd really like to be able to use previews and have each ...
10
votes
2
answers
1k
views
Is there an alternative to the ENABLE_PREVIEWS environment build variable for Xcode 16?
Up until Xcode 16, it was possible to check in build scripts of your Xcode project via the ENABLE_PREVIEWS environment variable whether a build for a SwiftUI preview is being executed.
This made it ...
1
vote
0
answers
202
views
Using SwiftUI previews on real device within SPM package code
I am trying to modularize my iOS project and extract a feature as a module using SPM. This module contains some SwiftUI code that I want to test with Xcode Previews on a real device. The preview works ...
1
vote
1
answer
553
views
SwiftUI Preview for a View with generic views passed as parameters
I would like to use SwiftUI previews for my view, which is built as a container that contains other Views. For example,
public struct ContainerView<Content: View>: View {
private let title: ...
1
vote
0
answers
307
views
SwiftUI Preview crashes with SwiftData on nested SwiftData array
Working with SwiftData and it seems when I try to access an array within the model Previews crash. There is no error message, just a black preview and the "Preview Crashed" message (opposed ...
2
votes
0
answers
516
views
Why is my Xcode canvas not showing any device?
EDIT: I solved this issue a while ago by just updating xcode lol. Thought I would update this answer in case anybody comes across it
I am using Xcode version 14.3.1 and my canvas has randomly stopped ...
1
vote
2
answers
887
views
Detect if build is triggered from SwiftUI Preview in Build Phases - Run Script
Is there any way to detect if the build was triggered by the SwiftUI Preview from inside Build Phases - Run Script?
I'm using swiftlint with --fix --format, the problem is when I have the Preview open ...
1
vote
1
answer
600
views
Fonts in Swift Package break Previews, why?
Problem
It appears that fonts, added to a swift package, somehow breaks my Previews/Canvas. It should be noted that I'm working with an acircular dependency graph, meaning almost everything is ...
0
votes
1
answer
480
views
cannot preview in this file - the request to open "com.apple.dt.CcodePreview" failed
Xcode version 14.3.1
Minimum Deployments 16.4
The problem started when I moved the Xcode to Applications(previously it was in somewhere else).
4
votes
0
answers
190
views
Share data with multiple SwiftUI Preview macros without data remaining in Release build config
I am looking for how to do the equivalent of this:
#Preview("light") {
BillView(bill: prevBill)
}
#Preview("dark") {
BillView(bill: prevBill)
.preferredColorScheme(...
0
votes
1
answer
217
views
Amplify - Xcode Previews
I'm trying to get previews in Xcode 15 however it always fails to load, showing the following issue:
I'm getting the following error in my AppDelegate:
could not find module 'Amplify' for target '...
3
votes
0
answers
687
views
Declare Static var in SwiftUI #Preview macro
I have the following code in SwiftUI preview macro.
#Preview {
@StateObject static var store = Store()
...
}
And it fails with the error:
Static properties may only be declared on a ...
2
votes
1
answer
1k
views
Do Xcode 15 UIKit views with storyboards work with #Preview macro?
I'm just trying to get #Preview to work with a UIKit ViewController.
See screenshot for details. Nothing special, IMHO instantiating the VC in the #Preview should just work. But all of the graphical ...
0
votes
1
answer
1k
views
SwiftUI Preview not rendering
I am trying to use SwiftUI in my project where I was using UIKit only, until now. The problem is that Preview in Xcode is not working even for very simple View with Text("Hello World"). Code ...
-2
votes
2
answers
262
views
SwiftUI - Issues with Setting Data in EnvironmentObject for Preview
I have this overlay view and when a button is pressed it's supposed to refresh the view by calling a function in the view model. The code does work on the app itself but doesn't work within the ...
4
votes
1
answer
3k
views
SwiftUI Preview macro not working with Observation
Xcode Version 15.0 (15A240d)
IOS 17
Workstation - MacBook Pro 14 inch 2021 with M1 Pro CPU
Summary:
I have been using the Observable Object protocol with no issues, including previews. But now with ...
0
votes
0
answers
68
views
Preview is not loading data from Mock Json
Here is my PostListView.
struct PostListView: View {
@StateObject private var viewModel = PostViewModel(service: PostService())
var body: some View {
NavigationView {
VStack {
...
0
votes
1
answer
2k
views
In Xcode 14.3.1 using SwiftUI and preview, why doesn't the preview display both of my Views? (It shows them on separate previews)
My app is made up of two Views.
When I run it on the emulator, it looks like the following (minus the red bar - I added that to the image to make explaining this a bit easier):
All of the controls ...
0
votes
2
answers
2k
views
Make SwiftUI previews working with view models
I have a SwiftUI view with a view model associated to it.
struct BookmarksView: View {
@StateObject private var viewModel = BookmarksViewModel()
var body: some View {
switch ...
4
votes
0
answers
260
views
Previewing SwiftUI toolbars on macOS
Is there any way to preview the toolbar for a macOS SwiftUI app? I can't figure out how to structure my preview to get it to display the toolbar items in a view. It does the right thing when actually ...
5
votes
0
answers
1k
views
Is there a way to have SwiftUI Previews ignore specific files to prevent another build?
We have an MLPackage file in our SwiftUI project. Whenever we try to load Previews on a View file, Xcode gets stuck in an infinite loop of "Build for Previews". The builds succeed, but ...
0
votes
1
answer
278
views
Does @StateObject work for static properties, such as SwiftUI Previews?
I'm not sure if this is a bug or not, but from what I can tell, @StateObject seems to behave like @ObservedObject when used with static properties.
Expected behaviour: A single instance is made, that'...
7
votes
0
answers
672
views
Prevent main AppKit app window from opening when loading a SwiftUI preview
I have a Mac App that's mostly written in AppKit. Over time, I'm adding new views in SwiftUI, and all the while I've been using SwiftUI previews to preview both my new SwiftUI views, and my old AppKit ...
6
votes
2
answers
5k
views
How to print in SwiftUI Previews?
I know how to print in a SwiftUI view. I would like to do the same within a SwiftUI preview so that I don't need to run the app in the simulator to see the logs.
For example here I'm trying to print a ...
4
votes
0
answers
1k
views
How to optimize SwiftUI previews?
Is There a way to optimize SwiftUI preview builds so it can be done faster ?
I have a very big project and each time I need to do a View preview it would take forever building the entire project
For ...
5
votes
0
answers
149
views
Preview locale does not work for Xcode 14.1
According to the documentation, we should be able to set the locale on previews and the following code should work:
import SwiftUI
struct TestView: View {
var timeFormatter: DateFormatter {
...
0
votes
1
answer
133
views
UIKit in Preview canvas doesn't comply with the option previewLayout
I'm currently using UIKit for making views and now I want to preview it just like SwiftUI.
But my canvas preview looks like a device rather than the size of view itself.
What have I done wrong?
import ...
0
votes
1
answer
383
views
SwiftUI Preview Error: "Cannot preview in this file (Update failed)
Everytime I attempt to use SwiftUI previews I am greeted with this error. I am running Xcode 14.1 on Ventura 13.0.
30
votes
2
answers
10k
views
SwiftUI preview layout: size that fits does not work
I want my preview to only have the size of the view and not show the whole iPhone.
I have already tried it with this code:
struct TimerCardView: View {
var body: some View {
Text(&...
2
votes
0
answers
636
views
SwiftUI Previews building different schemes/targets
I am trying to optimize my SwiftUI preview build times.
In my project I have two schemes, one for dev and one for prod, with different configurations. When I kick off a SwiftUI preview build, both ...
7
votes
2
answers
2k
views
Swift UI previews in modular app with frameworks
Having an issue running the preview for SwiftUI views inside my framework.
Here is a project structure.
Preview for SwiftUI views that are in the main target ( ContentView, GradientView) work as ...
1
vote
2
answers
203
views
Making Complex JSON Data Codable with Swift
I know this isn't totally SwiftUI related but please bear with me.
Using this excellent video by Karin Prater https://www.youtube.com/watch?v=ZHK5TwKwcE4
I have built an app using the spaceX API here :...
4
votes
1
answer
953
views
Xcode - SwiftUI Preview Inside SPM Package Error "Could not find host for previews"
I am using a SPM package and added one simple DashboardView inside the package as below:
import SwiftUI
struct DashboardView: View {
var body: some View {
Text("Hello World")
...
8
votes
0
answers
1k
views
Xcode can't generate previews with Swift Package
I'm getting this error because of a Swift package I have added to my project.
HumanReadableSwiftError
SettingsError: noExecutablePath(<IDESwiftPackageStaticLibraryProductBuildable:ObjectIdentifier(...
3
votes
2
answers
4k
views
Xcode 14 - Cannot preview in this file, Cannot connect to iPhone 14 | FailedToConnectToDTServiceHubError: Cannot connect to iPhone 14
while exploring the swiftUI and creating my first swiftUI app in Xcode, it is throwing an error for the preview saying - Cannot preview in this file, Cannot connect to [Device Name]. While it's ...
2
votes
1
answer
3k
views
Preview isolated SwiftUI views in a widget with a custom size
One of the big strengths of SwiftUI is modularity: I don't need to build one large view (controller), I can easily compose my main view from many smaller view components. But developing these view ...
2
votes
0
answers
650
views
SwiftUI dismiss() does not work on preview
I am making a TODO app on Xcode (ver 13.4) on mac.
When TodoEditor's "Add" button is tapped, the preview screen should show a ContentView and the ContentView should show updated Todo ...
2
votes
1
answer
666
views
SwiftUI preview fails to build when using enum as namespace
Since Swift has no real first-class namespace support, enums are often used as a replacement. When doing so, SwiftUI previews don't build anymore:
import SwiftUI
enum Namespace { }
extension ...
3
votes
1
answer
669
views
How to show SwiftUI preview in landscape
.previewLayout(.fixed(width: 480, height: 320)) has no effect in Xcode 13.4 and Xcode 14 beta 3
The preview is shown in portrait vs expected landscape
0
votes
0
answers
390
views
How to get UICollectionViewCell to size properly when using SwiftUI PreviewProvider
SwiftUI Previews are great and quickly allow us to preview views. I know its possible to create a wrapper to preview UIKit views, however I’m having trouble getting a CollectionViewCell preview to ...
0
votes
2
answers
370
views
SwiftUI & Unit test fails: Can't add the same store twice when using core data stack
After updating Xcode to 13.4 my core data persistence stack started to fail for SwiftUI previews and Xcode Unit tests.
Error message:
Unresolved error Error Domain=NSCocoaErrorDomain Code=134081 "...
1
vote
1
answer
1k
views
Preview SwiftUI private non-default @State
SwiftUI state properties should be declared as private. This is good for encapsulating their values from containing views, but prevents a preview from setting a non-default state using the default ...
1
vote
1
answer
943
views
SwiftUI Previews w/Mock Realm
I am running into a problem trying to use an in memory realm configuration with previews. When the preview executes it returns an error. The diagnostics gives the following error:
MessageError: ...
4
votes
1
answer
4k
views
Xcode SwiftUI Previews build settings
I am running a pre-build script using scheme. My script has enabled option to provide build settings from target that is being built. This script also gets executed when project is built for SwiftUI ...
4
votes
0
answers
256
views
SwiftUI preview failure, Failed to launch swiftc: 'swiftc too many arguments' reason?
I have a large mixed language (obj-c, swift & c++) project, it builds and runs fine on all platforms
"Build for previews" also seems to complete fine.
Opening preview of a particular ...