Jason
Jason

Reputation: 2333

Xcode SwiftUI Canvas "Content View Previews" not found in any targets

I see others post with issues using SwiftUI Previews in Xcode 11 and 12, but have not seen this issue elsewhere.

Older project that worked in Xcode 11, loaded in Xcode 12, fails to launch the preview, no matter how simple the Preview content.

Actual preview code:

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Text("I heart Xcode")
            .previewDevice("iPhone 11")
    }
}

After clicking "Resume" or "Try Again":

CouldNotFindInputNodeInBuiltGraphError<XcodeGraphSource, PreviewInstance>: Preview 1 of "Content View Previews" not found in any targets

Preview 1 of "Content View Previews" must belong to at least one target in the current scheme in order to use previews

Element: PreviewInstance(destinationDescription: UVPreviewPipeline.AnyPreviewDestinationDescription(storage: UVPreviewPipeline.(unknown context at $1382211e0).AnyPreviewDestinationDescriptionBoxImpl<UVIntegration.SimulatorPreviewDestinationDescription>), previewIdentity: UVPreviewPipeline.PreviewIdentity(identifier: ContentView_Previews[0], description: contentType: Element contextType: application device: iPhone 11 index: 0 layout: device supportsLive: true supportsOnDevice: true), variant: nil, variantedIdentifier: ContentView_Previews[0], identifier: AnyHashable(ContentView_Previews[0]))

I only have one target, same as before, with no special configuration. I have tried reloading Xcode as well as copying all sources files to a new directory (saw that one somewhere).

Anyone else able to parse this error message or make a recommendation for how to fix SwiftUI previews?

Thanks.

Upvotes: 28

Views: 14620

Answers (13)

Santiago
Santiago

Reputation: 588

Just reopen your xcode can fix this issue, it happens when you move your files relative path like into a new folder..

Upvotes: 0

mushcraft
mushcraft

Reputation: 2234

My scenario and resolution is different for the accepted answer.

I encountered this error today when I moved all my views into a 'Views' group folder. Then my latest view would not generate a preview and xcode displayed the error reported.

Preview provider "Your View" must belong to at least one target in the current scheme in order to use previews

This error was pointing to an existing view I wasn't currently working on, but it was an opened tab.

I closed all the tabs with views that I had open prior to file restructuring. I then reopen the view I was working on and xcode began redisplaying previews.

Upvotes: 80

jjj
jjj

Reputation: 1

If you change the name of your basic/default struct ContentView to e.g. MyView you also need to rename it at the top of the file. Just change ContentView.swift to MyView.swift

Upvotes: 0

Anup Kumar Mishra
Anup Kumar Mishra

Reputation: 206

This error occurs whenever we move the View File one place to another place simply you Need to Restart the Xcode then check the Preview section in my case it is successfully run.

Upvotes: 0

Mak
Mak

Reputation: 19

I had the same error. FIX: Close all XCode tabs, clean data, and restart xcode

Upvotes: 1

Pavel Paddubotski
Pavel Paddubotski

Reputation: 127

I just closed the tab and reopened it and it started to render.enter image description here

Upvotes: 0

Starsky
Starsky

Reputation: 2028

In my case, I have a separate package where I add reusable components built with SwiftUI, which later can be used in the project. I had this error with Previews, until I noticed that my current selected target in XCode was not the target of the current package where I was working on.

Selecting that to the current one fixed the problem.

enter image description here

Upvotes: 4

xgqfrms
xgqfrms

Reputation: 12136

same error "Content View Previews" not found in any targets

the error occurred, after I moved ContentView.swift file to an folder App

enter image description here

my solution ✅

just needs to Restart your Xcode, and everything should be fine!

enter image description here

Upvotes: 21

B. Mohammad
B. Mohammad

Reputation: 2464

I had this issue after moving some views to group folders, the issue was resolved after restarting Xcode.

Upvotes: 1

Victor Sebastian
Victor Sebastian

Reputation: 197

Removing the reference and adding the file back to the Xcode worked for me.

Upvotes: 1

Ren&#234; X
Ren&#234; X

Reputation: 69

My case, I moved to another group folder, but I solved clicking on: file right click > Delete > Remove Reference

Then I added back again to the project: file/folder right click > Add Files to "Project Name"...

Upvotes: 3

Akash Shindhe
Akash Shindhe

Reputation: 578

Preview provider "Your View" must belong to at least one target in the current scheme in order to use previews

I had this showing up . This came up when i moved my project to another system by compressing and transfer.

It started working after - i removed the pinned preview . enter image description here

Upvotes: 7

Hardy
Hardy

Reputation: 4796

By chance, I recognized why the previews were broken in my from-scratch-project. Reason was the "Versioning" build phase that I originally added during setup:

xcrun agvtool next-version -all

After wrapping that into a Previews check, everything's fine again:

if [ $ENABLE_PREVIEWS == "NO" ]
then
    xcrun agvtool next-version -all
fi

Upvotes: 5

Related Questions