MadeByDouglas
MadeByDouglas

Reputation: 2835

Xcode 11 beta swift ui preview not showing

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

Upvotes: 75

Views: 60132

Answers (12)

Neelu M
Neelu M

Reputation: 1

import SwiftUI

struct ContentView: View { var body: some View { VStack { Text("Hello, world!") } .padding() .padding(8) .background(.yellow) } } #Preview { ContentView() }

Upvotes: -1

Vivek
Vivek

Reputation: 599

Alt + Command + Enter

Will solve the issue

Upvotes: 3

Divesh singh
Divesh singh

Reputation: 437

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

Upvotes: -1

Saranjith
Saranjith

Reputation: 11567

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.

Upvotes: 67

Dave Batton
Dave Batton

Reputation: 8845

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

Upvotes: 15

Mojtaba Hosseini
Mojtaba Hosseini

Reputation: 119312

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

Upvotes: 25

Jadian
Jadian

Reputation: 4181

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

Upvotes: 364

jpayoung
jpayoung

Reputation: 11

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.

Upvotes: 1

SaRaVaNaN DM
SaRaVaNaN DM

Reputation: 4410

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)

Upvotes: 0

casillas
casillas

Reputation: 16793

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.

Upvotes: 24

atalayasa
atalayasa

Reputation: 3480

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())

Upvotes: 15

Elshad Karimov
Elshad Karimov

Reputation: 322

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

Upvotes: 10

Related Questions