Hello!
Being a beginner in creating and editing views using swiftUI, I am unsure of the current situation that I am presently in, and would be grateful for any advice or suggestions for the below problem.
For some context, I have recently begun exploring the "Capturing depth using the LiDAR camera" Documentation using AVFoundation and intend to change its ContentView file which is currently written in swiftUI.
The problem: I am unable to preview the SwiftUI code.
When I try to resume the Preview Pane on the right
Xcode Previews
RSS for tagCreate an app UI and configure almost everything your users see using Xcode Previews.
Posts under Xcode Previews tag
139 Posts
Sort by:
Post
Replies
Boosts
Views
Activity
I'm working on a horizontally scrollable header view. I'd like to implement the following behavior: when users tap on the left/rightmost header items, the header would scroll so they become fully visible. I am using a custom PreferenceKey to report the selected index:
struct SelectedTabPreference: PreferenceKey {
static var defaultValue: Int = 0
static func reduce(value: inout Int, nextValue: () -> Int) {}
}
Then in my view, I wrap my content inside a ScrollViewReader to scroll to the selected header:
struct ContentView: View {
let tabs: [DrawerTab]
@State private var selectedIndex: Int = 0
var body: some View {
ScrollViewReader { scrollProxy in
ScrollView(.horizontal, showsIndicators: false) {
TopTabView(options: tabs, selectedIndex: $selectedIndex)
.padding(32)
.background(Color.clear.preference(key: SelectedTabPreference.self, value: selectedIndex))
}
.onPreferenceChange(SelectedTabPreference.self) { value in
let firstTabSelected = value == 0
let lastTabSelected = value == tabs.count - 1
guard firstTabSelected || lastTabSelected else { return }
withAnimation {
let anchor: UnitPoint = firstTabSelected ? UnitPoint(x: 0.1, y: 0) : UnitPoint(x: 0.9, y: 0)
scrollProxy.scrollTo(tabs[value], anchor: anchor)
}
}
}
}
}
This works well when I run the app, but the Xcode Preview only shows a white screen. In fact, if I include this view as a subview in other views, their previews also break. The Preview only returns to life if I remove the programmatic scrolling:
scrollProxy.scrollTo(tabs[value], anchor: anchor)
I'm pretty sure this has to do with the ScrollViewProxy as my other Preference-based layouts work with Previews, but I don't know how to resolve the issue. Xcode Previews are an integral part of my workflow, and I'd like to avoid losing them if there's a chance.
I am unable to switch from iPhone 15(IOS 17 sim) to iPhone 14 pro(IOS 16.4 sim). I have both iOS 17 and iOS 16.4 simulators installed
p.s : Trying to switch to IOS 16.4 sim because 17 causes my M2 Air overheat alot to the extent that i can't touch it.
Hi, 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 is correct and project is possible to build and run. Even when I try to select real device (real iPhone) I can see Preview in my device. But preview in simulator is not working. Can someone help me with this? I am attaching diagnostic from Xcode and file from ~/Library/Logs/DiagnosticReports/
Thanks for help
xcode_preview.txt
findmylocated-2023-11-08-133122.ips.txt
Hi all,
when I am trying to build a preview I am always getting the error "Failed to launch app in reasonable time". When clicking on Diagnostics I get the following report information:
AppLaunchTimeoutError: Failed to launch app ”SwiftUI-Weather.app” in reasonable time
The app ”SwiftUI-Weather.app” did not launch on ”iPhone 15 Pro” in 15 seconds.
bundle identifier: tregnet.SwiftUI-Weather
device name: iPhone 15 Pro
path: /Users/MyUser/Library/Developer/Xcode/DerivedData/SwiftUI-Weather-fvxpuqojpxxuqtbuzmjvlatlmuqy/Build/Intermediates.noindex/Previews/SwiftUI-Weather/Products/Debug-iphonesimulator/SwiftUI-Weather.app
Also clicking on "Generate Report" in the Diagnostics menu is not doing anything for me, even after waiting for a few minutes.
Any help would be appreciated.
Greetings
It seem that Xcode preview compiler fail correctly parse generic and produce a "Failed to build" error:
Proposed example:
^^^^^^^^^^^^^^^^^^^^^^^^^^^
import CoreBluetooth
import SwiftUI
/// Working in preview
/*
struct InitialView<S>: View where S: StringProtocol {
var body: some View {
NestedView()
}
struct NestedView: View {
var body: some View {
Text("Hello")
}
}
}
struct WorkingView: View {
var body: some View {
InitialView<String>()
}
}
*/
/// Not working in preview
struct NotWorking: View {
var body: some View {
InitialView<String>()
}
struct InitialView<S>: View where S: StringProtocol {
var body: some View {
NestedView()
}
struct NestedView: View {
var body: some View {
Text("Hello")
}
}
}
}
struct ContentView_PreviewProviders: PreviewProvider {
static var previews: some View {
NotWorking()
}
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^
investigating the SampleView.1.preview-thunk.swift from ~/Library/Developer/Xcode/DerivedData/.../ directory, I found that compiler translate the above code into pre-compiled stage using typealias without considering the Generic condition-
^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
extension NotWorking.InitialView.NestedView {
typealias InitialView = NotWorking.InitialView
typealias NestedView = NotWorking.InitialView.NestedView
@_dynamicReplacement(for: body) private var __preview__body: some View {
#sourceLocation(file: "/Users/giuseppe/Development/Private/Dev/MyPlayground/MyPlayground/SampleView.swift", line: 40)
Text(__designTimeString("#32812.[2].[1].[1].[0].property.[0].[0].arg[0].value", fallback: "Hello"))
#sourceLocation()
}
}
extension NotWorking.InitialView {
typealias InitialView = NotWorking.InitialView
typealias NestedView = NotWorking.InitialView.NestedView
@_dynamicReplacement(for: body) private var __preview__body: some View {
#sourceLocation(file: "/Users/giuseppe/Development/Private/Dev/MyPlayground/MyPlayground/SampleView.swift", line: 35)
NestedView()
#sourceLocation()
}
}
extension NotWorking {
@_dynamicReplacement(for: body) private var __preview__body: some View {
#sourceLocation(file: "/Users/giuseppe/Development/Private/Dev/MyPlayground/MyPlayground/SampleView.swift", line: 30)
InitialView<String>()
#sourceLocation()
}
}
...
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Should be considered a compiler bug or I missed something in my code?
SwiftUI preview hangs on "Preparing iPhone Simulator for Previews". Also simulator hangs when trying to build and run. This occurred after I installed iOS 17 simulator. I tried reverting back to an iOS 16 version of the simulator, but haven't figured out how to do that. Build and run to an iPhone device works fine. Any ideas would be appreciated. Thanks.
Summary
When trying to display SwiftUI previews, building the previews may fail with the following error:
Linking failed: linker command failed with exit code 1 (use -v to see invocation)
ld: warning: search path '/Applications/Xcode.app/Contents/SharedFrameworks-iphonesimulator' not found
ld: warning: Could not find or use auto-linked framework 'CoreAudioTypes': framework 'CoreAudioTypes' not found
Note that may app does not use CoreAudioTypes.
Observation
This issue seems to occur when two conditions are met:
The SwiftUI view must be located in a Swift Package
Somewhere in either the View or the #Preview a type from another package has to be used.
Say I have to packages one named Model-package and one named UI-Package. The UI-Package depends on the Model-Package. If I have a SwiftUI view in the UI-Package that uses a type of the Model-Package either in the View itself or in the #Preview, then the described error occurs. If I have a View in the UI-package that does not use a type of the Model-Package anywhere in its View or #Preview then the SwiftUI Preview builds and renders successful.
I created a bug report: FB13033812
The Xcode Canvas Simulator (for Xcode 14 and even 15 ..beta 6) cause multiple springboard and diagnosticd processes to run at nearly 100% CPU , cause the MacBook M1 to heat. This happens every time.
This is usually triggered after the Canvas show an error which live previewing the code as I edit it.
I have tried clearing the derived data but it doesn't seem to help. This happens with all projects. I am unable to use live previews because of this issue.
The are often other processes with the string Poster in them. Like PhotosPosterProvider , CollectionsPoster , ExtragalacticPoster running high on usage at the same time.
Hi guys,
I am migrating my widgets to iOS 17 and because I already manage my layout margins, I just want to disable to new built-in widget content margins.
I did it by using ".contentMarginsDisabled()" on the WidgetConfiguration and it works fine at run time.
WIDGET CODE
struct MyWidget: Widget {
let kind: String = "MyWidget"
var body: some WidgetConfiguration {
return IntentConfiguration(kind: kind, intent: MyWidgetIntent.self, provider: WidgetProvider<MyWidgetIntent>()) { entry in
WidgetView<MyWidgetIntent>(entry: entry)
}
.configurationDisplayName("My Widget")
.supportedFamilies([WidgetFamily.systemMedium])
.contentMarginsDisabled() // <-- HERE
}
}
RESULT
Nevertheless, when previewing the WidgetView in a WidgetPreviewContext I didn't find anyway to disable the content margins (because manipulating directly the view and not a WidgetConfiguration).
PREVIEW CODE
struct MyWidget_Previews: PreviewProvider {
static var previews: some View {
WidgetView<MyWidgetIntent>(entry: WidgetEntry<MyWidgetIntent>())
// .padding(-16) Need to add this negative padding to disable margin
.previewContext(
WidgetPreviewContext(family: .systemMedium))
}
}
Do you know how to disable the content margins for widget preview?
Hello,
In WidgetKit, I have to write multiple #Preview macros for each family the widget supports. So is there any better way to write the #Preview?
(Although I can use the legacy PreviewProvider but it does not support timeline to test transition animation.)
#import WidgetKit
#import SwiftUI
struct DailyCaffeineWidget: Widget {
...
}
@available(iOS 17.0, *)
#Preview("Inline", as: .accessoryInline) {
DailyCaffeineWidget()
} timelineProvider: {
previewTimelineProvider()
}
@available(iOS 17.0, *)
#Preview("Circular", as: .accessoryCircular) {
DailyCaffeineWidget()
} timelineProvider: {
previewTimelineProvider()
}
@available(iOS 17.0, *)
#Preview("Rectangular", as: .accessoryRectangular) {
DailyCaffeineWidget()
} timelineProvider: {
previewTimelineProvider()
}
@available(iOS 17.0, *)
#Preview("Small", as: .systemSmall) {
DailyCaffeineWidget()
} timelineProvider: {
previewTimelineProvider()
}
@available(iOS 17.0, *)
#Preview("Medium", as: .systemMedium) {
DailyCaffeineWidget()
} timelineProvider: {
previewTimelineProvider()
}
...
Following article Creating a widget extension on developer.apple.com (https://developer.apple.com/documentation/widgetkit/creating-a-widget-extension) I encountered a problem with XCode preview: it shows an error with message
| RemoteHumanReadableError: Failed to launch agent
| No plugin is registered to launch the process type widgetExtension.
Can someone help me?
macOS 13.4
XCode 14.3.1
Hey guys, anybody knows how to fix issues with the keyboard on Xcode in preview mode? it used to work well for me but has now stopped working, see details.
Problem statement: Cannot use the keyboard in previews. For example, typing in a textfield does nothing. Everything works well in simulator.
Environment: macOS Monterey 12.6
Xcode 14.1
Thanks in advance for your input.
HI,
I have an issue displaying SwiftUI previews with Xcode 14.
My project has iOS and an watchOS Target. It also includes a lot of SPM, most of them only used by the PhoneApp. The project build successfully for both iOS and watchOS.
I can see that when building the watch target, only SPM that are included in this target are build for for watchOS
The issue is when I try to build a swiftUI preview of the watch target. In this case I can see on the build for Preview log that Xcode try to build ALL spm defined in the Xcode project, including the one that are not used by the watch Target,
As a lot if spm are for iOS only, and doesn't build for watchOS, the build for preview fails, and I'm unable to preview my swiftUI views on the AppeWatch.
Note that my project is working fine on Xcode 13.
This issue is easy to reproduce in any Xcode projects containing a iOS and watchOS target by adding a SPM that is only for iOS.
Any workaround to fix this issue ?
The Build for Previews builds all my targets including the test targets. Is there a way to configure the relevant targets? I do not see an option in the schema editor, and disabling Find Implicit Dependencies has no effect either.
SwiftUI Preview Don't work.
Progress Preparing iPhone Simulator for Previews.
SwiftUI
Xcode version 13.0
macOS Big Sur version 11.6
I have an iOS app by using a swift package to hold most of the logic. However, the previews of SwiftUI views often fail with following error:
HumanReadableSwiftError
BuildError: failedToGenerateThunkInfo(Error Domain=com.apple.xcbuild Code=19 "could not generate preview info: noTargetBuildGraph" UserInfo={NSLocalizedDescription=could not generate preview info: noTargetBuildGraph})
To reproduce this, please clone https://github.com/pointfreeco/isowords and check for preview of ChangelogView.
I have two (local) Swift packages (both with a single library product): RemoteImage, which defines setImage(from:) function on UIImageView and SatelitUI package which directly depends on the first one and defines some views. But when I'm trying to preview views from the second package I'm getting the following error:
linker command failed with exit code 1 (use -v to see invocation)
LinkDylibError: Failed to build TrailerView.swift
Linking failed: linker command failed with exit code 1 (use -v to see invocation)
ld: warning: directory not found for option '-F/Applications/Xcode-beta.app/Contents/SharedFrameworks-iphonesimulator'
Undefined symbols for architecture x86_64:
"(extension in RemoteImage):__C.UIImageView.setImage(from: Foundation.URL?) -> ()", referenced from:
(extension in SatelitUI_PreviewReplacement_TrailerView_2):SatelitUI.TrailerView.(previewupdate in _8C3731B0EF007627509BEEB93277D681)(with: SatelitUI.Trailer?) -> () in TrailerView.2.preview-thunk.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Apparently, Xcode fails to link the library from the first package because it's dynamic. Static linking works as expected.
It's a bug I believe?
I'm trying to preview a view from a swift package which is located in one of the target's directory subdirectories:
.
├── Package.swift
├── README.md
└── Sources
		└── SatelitUI
				└── Views
						└── MenuBar.swift
When I'm previewing a view from the MenuBar.swift file I'm getting following error:
NoBuildableEntriesError: active scheme does not build this file
Select a scheme that builds a target which contains the current file, or add this file to a target that is built by the current scheme.
Which is unexpected because the file should be a part of the SatelitUI target. The thing is it works just fine if I move it into the root target's directory (SatelitUI).