73

Just playing with Swift UI basic app and the preview canvas is not showing even though I'm in canvas mode. App runs, and I have this little snippet what am I missing?

#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
       ContentView()
    }
}
#endif
MadeByDouglas
  • 2,509
  • 1
  • 18
  • 22
  • 7
    Ensure that your Mac is running macOS 10.15 beta – Rohit Makwana Jun 20 '19 at 10:17
  • Possible duplicate of [Xcode Canvas for SwiftUI previews does not show up](https://stackoverflow.com/questions/56435054/xcode-canvas-for-swiftui-previews-does-not-show-up) – pkamb Apr 21 '22 at 19:08

11 Answers11

362

Maybe someone is looking for this: Editor -> Canvas (Alt + Command + Enter)

Jadian
  • 4,144
  • 2
  • 13
  • 10
  • 5
    1 hour looking for this button. It makes me wonder who's the blessed developer who placed it there. – Cristian Dec 17 '20 at 16:12
67

To open the preview canvas, just go to Editor -> Canvas

Or do Alt + Command + Enter

If your macOS version is below 10.15, you will get the below error

enter image description here

Update mac and try the same.

shreyasm-dev
  • 2,711
  • 5
  • 16
  • 34
Saranjith
  • 11,242
  • 5
  • 69
  • 122
25

From the editor:

Top-Right corner of the editor, and then:

enter image description here


From the menu

Editor -> Canvas

enter image description here


Shortcuts

Open Canvas

⌥ option + ⌘ command + ↩ return

Refresh canvas

⌥ option + ⌘ command + P


A note about the macOS 

In the mac environment, you can search for any menu option under the Help menu, then it will open the exact menu path you are looking for.

For example, searching for canvas result in this:

enter image description here

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
24

To preview and interact with views from the canvas in Xcode, ensure your Mac is running on Catalina MacOS.

https://developer.apple.com/tutorials/swiftui/creating-and-combining-views

Please check apple document in the following URL https://developer.apple.com/documentation/xcode_release_notes/xcode_11_beta_2_release_notes

Xcode 11 beta supports development with SwiftUI.

Note

Tools for SwiftUI development are only available when running on macOS Catalina 10.15 beta.

casillas
  • 16,351
  • 19
  • 115
  • 215
  • 9
    I wonder why they are forcing you to upgrade an entire OS just to be able to use some newer version of an SDK. – meh Sep 18 '19 at 11:10
15

Additionally, if you are using macOS system older than beta version like 10.14 you can use Xcode playground to preview with following code.

import PlaygroundSupport
import SwiftUI

    struct ContentView : View {
        var body: some View {
            Text("Hello World")
        }
    }
 PlaygroundPage.current.liveView = UIHostingController(rootView: ContentView())
atalayasa
  • 3,310
  • 25
  • 42
  • 1
    it gives this error - No such module 'PlaygroundSupport' – Elshad Karimov Jun 20 '19 at 12:40
  • Is it possible that you have created Playground for macOS or something else? Can you try to create new Playground @ElshadKarimov – atalayasa Jun 20 '19 at 13:07
  • Catalina + Xcode 11b3 + Playground = Not working Preview – iTux Jul 12 '19 at 18:30
  • If you are using Catalina why you are trying to use Playground? My answer is people who is not using macOS 10.15 Catalina. @iTux – atalayasa Jul 16 '19 at 06:14
  • @AtalayAşa because I would not create app for iPhone or Mac, I was try SwiftUI :) After all tries I did create app for iOS with SwiftUI, and now is all good :) – iTux Jul 16 '19 at 15:20
15

I'm running Xcode 12.2 on Catalina 10.15.7, and I not only need to make sure Canvas is selected but then I need to select Create Preview from the Editor menu.

Xcode Editor menu

Dave Batton
  • 8,795
  • 1
  • 46
  • 50
  • Turns out this is necessary if you create a Swift file, but not if you create a SwiftUI file. I think a SwiftUI file is just a Swift file plus it creates the preview for you. – Dave Batton Jan 06 '21 at 16:56
10

To preview and interact with views from the canvas in Xcode, ensure your Mac is running macOS 10.15 beta.

Elshad Karimov
  • 322
  • 2
  • 10
2

Alt + Command + Enter

Will solve the issue

Vivek
  • 566
  • 5
  • 6
1

In Xcode 11 Beta 6, there is a known issue and workaround for views that do not have their certain flags set (see below).

If this applies to you, and you are unable to set the corresponding flags, you may need to remove the #if debug flag around ContentView_Previews until the issue is fixed in a future Xcode release.

See the Xcode 11 Beta 6 Release Notes:

Preview providers that are defined in projects which don’t have -DDEBUG set for OTHER_SWIFT_FLAGS and wrapped in #if DEBUG don’t display in the canvas. That happens, for example, in Objective-C apps which that never had occasion to specify OTHER_SWIFT_FLAGS. (51138834)

Workaround: Remove the #if DEBUG and #endif from around the PreviewProvider declaration.

jpayoung
  • 11
  • 4
0

Check this xcode_11_beta_5_release_notes https://developer.apple.com/documentation/xcode_release_notes/xcode_11_beta_5_release_notes .

With Xcode 11 beta 5, previews are only available on macOS Catalina 10.15 beta 5, and only Xcode 11 beta 5 supports previews on macOS Catalina 10.15 beta 5. (52059562)

SaRaVaNaN DM
  • 4,390
  • 4
  • 22
  • 30
0

I have restarted the Xcode and machine and it's worked for me. You can try this for many problems.

Divesh singh
  • 409
  • 4
  • 12