TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

The view is only updating in the button

Forums > SwiftUI

Hi i have a page that has a login screen and everything in the page works, but when I click on the login button the new view only replaces the button and if I try to set a background then the button just disappears. I have tried to look at other places but I cannot find anything so if anyone can help with this that would be great. Here is my code:

//
//  ContentView.swift
//  
//
//  Created by 
//

import SwiftUI
import UIKit

struct ContentView: View {

    @State private var username = ""
    @State private var password = ""
    @State private var wrongUsername = 0
    @State private var wrongPassword = 0
    @State private var showingLoginScreen = false
    @State private var isLoggedIn = false

    var body: some View {
        GeometryReader { geometry in
            NavigationView {
                ZStack {
                    LinearGradient(
                        gradient: Gradient(colors: [
                            Color(red: 105/255, green: 212/255, blue: 138/255),
                            Color(red: 79/255, green: 175/255, blue: 123/255),
                            Color(red: 32/255, green: 119/255, blue: 85/255),
                            Color(red: 9/255, green: 82/255, blue: 54/255)
                        ]),
                        startPoint: .top,
                        endPoint: .bottom
                    )
                    .edgesIgnoringSafeArea(.all)
                    VStack {
                        Rectangle()
                            .frame(height: geometry.size.height * 0.1) // above logo
                            .foregroundColor(.clear)
                        Image("final")
                            .resizable()
                            .frame(width: 364, height: 201)
                        Rectangle() // below the logo above Log In
                            .frame(height: geometry.size.height * 0.03)
                            .foregroundColor(.clear)
                        Group {
                            HStack {
                                Text("Login")
                                    .font(.system(size: 25))
                                    .padding(.leading, geometry.size.width * 0.06)
                                    .padding(.bottom, 0)
                                    .foregroundColor(Color.white)
                                Spacer()
                            }
                            TextField("Username", text: $username)
                                .padding()
                                .frame(width: 345, height: 35)
                                .background(Color(red: 132/255, green: 211/255, blue: 180/255))
                                .cornerRadius(4)
                                .border(.red, width: CGFloat(wrongUsername))
                            SecureField("Password", text: $password)
                                .padding()
                                .frame(width: 345, height: 35)
                                .background(Color(red: 132/255, green: 211/255, blue: 180/255))
                                .cornerRadius(4)
                                .border(.red, width: CGFloat(wrongPassword))
                        }
                        Group {
                            Text("")
                                .padding(.bottom, 8)
                            if isLoggedIn {
                                            LoggedInView()
                                        } else {
                                            Button("LOGIN WITH YOUR BANK") {
                                                authenticateUser(username: username, password: password)
                                            }
                                            .bold()
                                            .foregroundColor(Color(red: 90/255, green: 84/255, blue: 84/255))
                                            .frame(width: 345, height: 35)
                                            .background(
                                                LinearGradient(
                                                    gradient: Gradient(colors: [
                                                        Color(red: 92/255, green: 191/255, blue: 121/255),
                                                        Color(red: 50/255, green: 182/255, blue: 88/255),
                                                    ]),
                                                    startPoint: .leading,
                                                    endPoint: .trailing
                                                )

                                            )
                                            .cornerRadius(4)
                                        }

                            Button("Reset Password") {

                            }
                            .foregroundColor(Color(red: 175/255, green: 198/255, blue: 170/255))
                        }
                        Rectangle()
                            .foregroundColor(.clear)
                        Group {
                            Rectangle() // above the or below password reset
                                .frame(height: geometry.size.height * 0.17)
                                .foregroundColor(.clear)
                            HStack {
                                Rectangle()
                                    .frame(width: 142, height: 1.5)
                                    .foregroundColor(Color(red: 162/255, green: 170/255, blue: 160/255))

                                Text("OR")
                                    .padding(.horizontal, 10)
                                    .foregroundColor(Color(red: 162/255, green: 170/255, blue: 160/255))
                                    .fontWeight(.semibold)

                                Rectangle()
                                    .frame(width: 142, height: 1.5)
                                    .foregroundColor(Color(red: 162/255, green: 170/255, blue: 160/255))
                            }
                            Rectangle() // between the or and the create account button
                                .frame(height: geometry.size.height * 0.05)
                                .foregroundColor(.clear)
                            Button("CREATE NEW ACCOUNT") {

                            }
                            .foregroundColor(Color.white)
                            .bold()
                            .frame(width: 345, height: 35)
                            .background(
                                LinearGradient(
                                    gradient: Gradient(colors: [
                                        Color(red: 54/255, green: 218/255, blue: 101/255),
                                        Color(red: 62/255, green: 201/255, blue: 110/255),
                                        Color(red: 46/255, green: 192/255, blue: 113/255)
                                    ]),
                                    startPoint: .leading,
                                    endPoint: .trailing
                                )
                            )
                            .cornerRadius(4)
                            Rectangle() // under the create account button
                                .frame(height: geometry.size.height * 0.07)
                                .foregroundColor(.clear)
                        }

                    }
                }
                .navigationBarHidden(true)
            }
        }
    }
    func authenticateUser(username: String, password: String) {
        print("Authenticating user...")

        if username.lowercased() == "kaitlin" && password.lowercased() == "123" {
            isLoggedIn = true
            print("Setting isLoggedIn to true")
        } else {
            print("Authentication failed")
        }
    }
}

struct LoggedInView: View {
    var body: some View {
        VStack {
            Text("You are logged in")
                .font(.title)
                .padding()
        }
        .navigationTitle("Logged In")
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

3      

Move

if isLoggedIn {
    LoggedInView()

to

  ZStack {
      LinearGradient(
          gradient: Gradient(colors: [
              Color(red: 105/255, green: 212/255, blue: 138/255),
              Color(red: 79/255, green: 175/255, blue: 123/255),
              Color(red: 32/255, green: 119/255, blue: 85/255),
              Color(red: 9/255, green: 82/255, blue: 54/255)
          ]),
          startPoint: .top,
          endPoint: .bottom
      )
      .edgesIgnoringSafeArea(.all)

      if isLoggedIn {
          LoggedInView()
      } else {
          VStack {
              Rectangle()
                  .frame(height: geometry.size.height * 0.1) // above logo
                  .foregroundColor(.clear)
                  // rest of code...

      } // do not forget to move this to after all the other code

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.