Learn Courses My Dashboard

Button Help For App

I am sorry for all the questions, I am starting to create my own app and I am needing to have a button that goes to the next page, but when I type in "butto"n on Xcode I don’t see that certain dropdown that you used under my dropdowns. Can someone please help me?

Do you mean the code completion dropdown like this screenshot?

yes, that is correct

Code completion may stop working for a number of reasons including if there is a syntax error somewhere else in your code.

Often the problem can be solved by cleaning your build folder. The build folder is where the compiler created the executable App to load onto the simulator you have chosen or enable you to run the code in the Preview canvas.

To clean the build folder you can use the Xcode menu via Product > Clean Build Folder or use the keyboard shortcut Shift + Command + K

Closing and restarting Xcode can also help bring code completions back to life or you may have to re-boot your Mac.

One more question, I am trying to add my “next” button and every time I type it in, in the preview, it always shows on top of my other text when I actually need it to show below my first text

"
stack {

        Image("birthday").padding(.all).ignoresSafeArea()
        
        VStack {
            Text("Welcome to Smilon")
                .fontWeight(.heavy)
                .foregroundColor(Color.black)
                .multilineTextAlignment(.center)
                .padding(.all)
            
        }
        Spacer()
        VStack {
            
        }
            
            Button(action: {}, label: {
                Text("Next")
                
                    })
                
                

        }
    
}

}

Is this what you are trying to achieve?

struct ContentView: View {

    var body: some View {
        VStack(spacing: 30) {
            Image("birthday")
                .resizable()
                .aspectRatio(contentMode: .fit)

            Text("Welcome to Smilon")
                .fontWeight(.heavy)
                .foregroundColor(Color.black)
                .multilineTextAlignment(.center)
                .padding(.all)

            Spacer()

            Button(action: {

            }, label: {
                Text("Next")
            })
        }
        .padding(.horizontal)
    }
}

Screenshot:

yes, it is

I did exactly what you did and I’m still getting the same problem
"
Spacer()

        Button(action: {
            
        }, label: {
            Text("Next")
                .fontWeight(.heavy)
                .foregroundColor(Color.black)
            
        })
    }
    .padding(.horizontal)
        }

I don’t know why it keeps happening

What does all the code look like in the View? Gotta be something simple that you are doing wrong

Paste your entire View code in as text but make sure you follow the instructions below.

To format the code nicely, place 3 back-ticks ``` on the line above your code and 3 back-ticks ``` on the line below your code. Like this:
```
Your code goes here
```
The 3 back-ticks must be the ONLY characters on the line. The back-tick character is located on the same keyboard key as the tilde character ~ (which is located below the Esc key). You can also highlight an entire code block and click the </> button on the toolbar to wrap the block for you.

This also makes it easier for anyone assisting as they can copy the code and carry out some testing.

‘’’
import SwiftUI

struct ContentView: View {

var body: some View {
    ZStack {
        Image("birthday")
            .aspectRatio(contentMode: .fill)
        
        Text("Welcome to Smilon")
            .fontWeight(.heavy)
            .foregroundColor(Color.black)
            .multilineTextAlignment(.center)
            .padding(.all)
        
        Spacer()
        
        Button(action: {
            
        }, label: {
            Text("Next")
                .fontWeight(.heavy)
                .foregroundColor(Color.black)
            
        })
    }
    .padding(.horizontal)
        }

struct ContentView_Previews: PreviewProvider {

static var previews: some View {
    ContentView()
        .previewLayout(.device)

.previewInterfaceOrientation(.portrait)
‘’’

Everything is in a ZStack so it puts the Views on top of each other.

Can you explain what you want the View to look like.

I’m wanting my view to look like the screenshot that you previously showed me, with the next button at the bottom of the screen

Then your code should look like this:

struct ContentView: View {

    var body: some View {
        VStack {
            Image("birthday")
                .resizable()
                .aspectRatio(contentMode: .fit)

            Text("Welcome to Smilon")
                .fontWeight(.heavy)
                .foregroundColor(Color.black)
                .multilineTextAlignment(.center)
                .padding(.all)

            Spacer()

            Button(action: {
                // Button action code goes here
            }, label: {
                Text("Next")
                    .fontWeight(.heavy)
                    .foregroundColor(Color.black)
            })
        }
        .padding(.horizontal)
    }
}

Copy all the code above and paste that in place of your struct ContentView

Actually, I meant to say I want the background to fill the whole phone screen and the “Welcome to Smilon” text to be in the center and the next button on the button

In that case the structure should be a ZStack in which you have the Image as the first View and then a VStack in which you have the Text() View and the Button(). To arrange the Text and the Button so that you have the Text in the middle you should make use of Spacer() elements above and below the Text.

So basically you have:

ZStack {
    Image()
    VStack {
        Spacer()
        Text()
        Spacer()
        Button()
    }
}

When i’m needing to add a new page to the app, that has quite a bit of text on it, how would I achieve that

Judging by the questions you are asking it seems like you are very new to SwiftUI.

What courses have you followed on SwiftUI to get you up to speed with creating Views and how to transition from one view to another?

I am very new to SwfitUI and I completed the 14 Day Beginners Challenge

OK so how did you go with each of the Challenges?

I did really well with majority of the challenges but the thing is that on that challenge, the app doesn’t have more than one page