I am trying to preview some View by using Widget context, like:
struct MyTasksView_Previews: PreviewProvider {
static var previews: some View {
MyTasksView(
myTasks: Fake.myTasks,
user: Fake.user,
error: ""
)
.previewContext(WidgetPreviewContext(family: .systemMedium))
}
}
However, I'm getting this error while attempt to run the preview. And, I'm not sure why it is happing.
RemoteHumanReadableError: Unknown preview provider “MyTasksView_Previews”
MyApp does not contain a preview provider named “MyTasksView_Previews”. Check your build settings to ensure the preview provider is compiled into your product.
I, also, tried to use a simple Text(text).previewContext(WidgetPreviewContext(family: .systemMedium)), but it did not work either. I'm using the Xcode beta 5.
In Apple Emoji Rangers Demo App for WWDC 2020. We can see this piece of code for preview:
struct CharacterNameView_Previews: PreviewProvider {
static var previews: some View {
CharacterNameView(CharacterDetail.panda)
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
I had the similar issue and I solved it by putting the View inside a VStack (or another container).
Here is my code that worked:
struct WidgetViewPreviews: PreviewProvider {
static var previews: some View {
VStack {
WidgetView(entry: .mock)
}
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
It's a bug in Xcode, you can only use previewContext of "single" Widgets Target.
So you need to unselect other Targets.
I had the same problem when adding widgets extension to my UIKit app.
Changing the Class target membership to widget extension solved this for me.
Add Widget extension target, and activate it. For more information
https://developer.apple.com/documentation/widgetkit/creating-a-widget-extension
Related
I am getting Closure containing a declaration cannot be used with result builder 'ViewBuilder' in swiftui mac application
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I Have a file called ContentView "Home Page" it is getting Closure containing a declaration cannot be used with result builder 'ViewBuilder' in swiftui mac application .
This error is coming. Click Me to see the image
I am using Navigation View
There are also other errors. I would Appreciate if you help me to fix all errors.
Hello heroes of the internet,
When we develop apps on Xcode we can preview apps on the Canvas, and it looks something like this:
Luckily, SwiftUI allows for splitting up the code in an easy manner, with "Views". Say you have a simple view and you want to preview it on a blank canvas, not on the phone with the phone's aspect ratio and notch and all that. I want to display my view in a simple blank canvas, like in the image below.
How can I do such thing?
Change to the selectable preview mode at the bottom of the canvas:
Then add this to the preview code:
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.previewLayout(.sizeThatFits) // here
}
}
Firstly, set up your preview layout in a fixed frame like below:
struct FContentView_Previews: PreviewProvider { //don't forget to change name
static var previews: some View { //don't forget to change name
FContentView()
.previewLayout(.fixed(width: 480, height: 320)) //this line of code
}
}
Then, set the preview option to Selectable like the below image.
You can also refer to my latest updated answer: fixed frame preview in canvas
Let's say my application has two pages; Login Page and Main Page. I created these two with SwiftUI View separately. How will I make the transition between them. I tried NavigationView but the second page opens with a NavigationBar (with the First Page named arrow). In summary, how can I switch between two views cleanly?
you can do this simply with switch or if/else :)
class Wnd : View {
#State var firstShown: Bool
var body: some View {
if firstShown {
FirstView(firstShown: $firstShown)
}
else
{
SecondView(firstShown: $firstShown)
}
}
}
firstView and secondView have Binding to firstShown var
Another one solution is to use handlesExternalEvents for switching views.
sample of usage: https://stackoverflow.com/a/65415593/4423545
Another solution is to hide NavigationBar:
.navigationBarHidden(true)
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello")
Text("World")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
The code above results in two preview windows: one with text "Hello", second window holds "World"
Every time I add something like Text() or whatever, it appears in the second preview window. Adding more elements automatically leads to new preview windows. THe code in the PreviewProvider remains the same, so it's not duplicate previews, it just the separate previews for EACH element...
I tried to restart Xcode, change simulator devices, create new projects - nothing changes.
It started after I added duplicate preview in one project and then deleted it by removing appropriate lines in the PreviewProvider. After that all new projects or all new files in old projects show this strange behaviour.
The body by default is a #ViewBuilder in SwiftUI 2.0, so it just generate a group of views inline, thus having two Text element you got two previews. If you want just one Preview your body should have the one top view, like
struct ContentView: View {
var body: some View {
HStack {
Text("Hello")
Text("World")
}
}
}
You have to put the two Text elements into a Container, like e.g. a VStack.
I've been successful using SwiftUI for a few months with very little Xcode experience. I'm trying to add some simple help screens that I would like to preview in the SwiftUI canvas. However, I'm receiving the following error:
OptimizationLevelError: not building -Onone
The message is shown when pressing the "diagnostics" button. Problem persists after pressing "try again" button.
Xcode: Version 11.3 (11C29)
Catalina: 10.15.2 (19C57)
The code could not be simpler:
import SwiftUI
struct HelpSortView: View {
var body: some View {
VStack {
Text("You can sort the list by Name, Sail Number, Class, or Favorites.")
.lineLimit(10)
VStack(alignment: .center) {
Image("help-sort")
}
}
}
}
struct HelpSortView_Previews: PreviewProvider {
static var previews: some View {
HelpSortView()
}
}
It turns out that I had my Xcode scheme, Build Configuration set to Release instead of debug. In Xcode, select Product, Scheme, Edit Scheme, Run (on left side), Info tab, Build Configuration, set drop-down to Debug.