skip to Main Content

This code gives an error when compiled, and it looks really simple. It should be a test screen for tabview in app. Maybe somebody knows what s wrong?

import SwiftUI

struct screenone: View {
    var body: some View{
        ZStack {
            Text ("Screen 1")
                . bold()
                .foregroundColor(.white)
        }
        frame(maxWidth: .infinity, maxHeight: .infinity)
            .background(.mint)
            .clipped()
    }
}

struct screenone_previews: PreviewProvider{
    static var previews: some View{
        screenone()
    }
} 

2

Answers


  1. Did you change ContentView() to Screenone() in your App struct? If not try replacing your App struct with the code below.

    import SwiftUI
    
    @main
    struct TestApp: App {
        var body: some Scene {
            WindowGroup {
                Screenone()
            }
        }
    }
    

    Once that is done replace your screenone struct with the following:

    import SwiftUI
    
    struct Screenone: View {
        var body: some View {
            ZStack {
                Text("Screen 1")
                    .bold()
                    .foregroundColor(.white)
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .background(.mint)
            .clipped()
        }
    }
    
    struct Screenone_Preview: PreviewProvider{
        static var previews: some View {
            Screenone()
        }
    } 
    

    The code above works for me.

    In the future please provide the specific error so that it is easier to address your problem!

    Login or Signup to reply.
  2. After your ZStack{} ended, you forgot to put a dot . before frame. Put the dot and run again. Follow the below code.

    ZStack {
            Text ("Screen 1")
                . bold()
                .foregroundColor(.white)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity) //added dot before .frame
        .background(.mint)
        .clipped()
    

    If this does not work, then you should include your error message here because let us assume your error is not the way to help you.

    Still, that . is surely the error.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search