0

this is my first post and I'm here because I've got a problem in my swiftUI code. Let me quickly explain: I have an application with a SignInView, which allows you to register on the application. Once you've given your credentials, you're redirected to the application when you're logged in. On my application, I have a view that lets you see your profile (photo, name, email, description, etc.). In this profile page, I've added the option of being able to modify the description with a Textfield. This is where the problem comes in, I don't know why, when I click on the textfield, the keyboard doesn't have time to open and it redirects me directly to my login page. I don't know why, it seems to happen when the keyboard opens. Here's a bit of my code from the 2 files to show you what I've done:

SignInView.Swift :

struct SignInView: View {
struct SignInView: View {
    init(){
        UINavigationBar.setAnimationsEnabled(false)
    }
    
    @StateObject var  routerModels = Router()
    @EnvironmentObject var signIn: SignIn
    @State private var colorWhite = Color.white
    @State private var colorBlack = Color.black
    @State private var showTextPassword = false
    @State private var missingFields = false
    @State var mail = ""
    @State var password = ""
    
    var body: some View {
        GeometryReader { geometry in
            NavigationView {
                ZStack {
                    BackgroundColorComponent()
                    VStack {
                        VStack {
// rest of code
                            VStack {
                                VStack(alignment: .leading) {
                                    Text("Nom d'utilisateur")
                                        .font(.system(size: geometry.size.height/50))
                                        .foregroundColor(colorBlack)
                                        .bold()
                                    HStack {
                                        TextField("", text: $mail)
                                            .onTapGesture {
                                                UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
                                            }
                                            .foregroundColor(colorBlack)
                                            .padding(.horizontal)
                                            .frame(width: geometry.size.height/2.8)
                                            .keyboardType(.default)
                                            .disableAutocorrection(true)
                                            .autocapitalization(.none)
                                    }
                                    Divider()
                                        .frame(width: geometry.size.height/2.4)
                                }.padding(.bottom, 30)
                                VStack(alignment: .leading) {
                                    Text("Mot de passe")
                                        .font(.system(size: geometry.size.height/50))
                                        .foregroundColor(colorBlack)
                                        .bold()
                                    HStack {
                                        if showTextPassword {
                                            TextField("", text: $password)
                                                .onTapGesture {
                                                    UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
                                                }
                                                .foregroundColor(colorBlack)
                                                .padding(.horizontal)
                                                .frame(width: geometry.size.height/2.8)
                                                .keyboardType(.default)
                                                .disableAutocorrection(true)
                                                .autocapitalization(.none)
                                        } else {
                                            SecureField("", text: $password)
                                                .onTapGesture {
                                                    UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
                                                }
                                                .foregroundColor(colorBlack)
                                                .padding(.horizontal)
                                                .frame(width: geometry.size.height/2.8)
                                                .keyboardType(.default)
                                                .disableAutocorrection(true)
                                                .autocapitalization(.none)
                                        }
                                        Button(action: {
                                            showTextPassword.toggle()
                                        }, label: {
                                            Image(systemName: showTextPassword ? "eye.slash.fill" : "eye.fill")
                                                .foregroundColor(colorBlack)
                                        })
                                    }
                                    Divider()
                                        .frame(width: geometry.size.height/2.4)
                                }
                            }
                            .padding(.vertical)
                            .padding(.horizontal)
                            .padding(.bottom, 60)
                            .background(colorWhite)
                            .cornerRadius(10)
                            
                            
                            NavigationLink(
                                destination: MainView(routerModels: routerModels).navigationBarHidden(true).environmentObject(LocationsViewModel()),
                                isActive: $signIn.isLoggedin) {
                                    Button(action: {
                                        if mail.count == 0 || password.count == 0 {
                                            missingFields = true
                                        } else {
                                            self.signIn.cekLogin(password: self.password, email: self.mail)
                                        }
                                    }) {
                                        Text("Connexion")
                                            .font(.title3.bold())
                                            .foregroundColor(colorWhite)
                                            .padding(.vertical, 22)
                                            .frame(width: geometry.size.height/3, height: geometry.size.width/6)
                                            .background(
                                                .linearGradient(.init(colors: [
                                                    Color("Button1"),
                                                    Color("Button2"),
                                                ]), startPoint: .leading, endPoint: .trailing), in: RoundedRectangle(cornerRadius: 20)
                                            )
                                    }.offset(y: -40)
                                        .padding(.bottom, -46)
                                }
                        }
// rest of code
                    }
                }
            }
        }
    }
}

ProfilView.Swift :

struct ProfilView: View {
    
    @ObservedObject var updateProfil = Profil()
    @EnvironmentObject var profil: Profil
    @State var user: UserInfo? = nil
    
    @State private var changeViewChangDesc: Bool = false
    @State var description = "(Description)"
    @State var showEditSheet = false
    @State private var showEditAlert = false
    @State private var editedDescription = ""
    @State private var showingImageModal = false
    @State private var profilPicture = UIImage(named: "avatar")!
    @State private var showingProfilPicture = false
    @EnvironmentObject var appTheme: AppTheme
    
    var body: some View {
        GeometryReader { geometry in
            ScrollView() {
// rest of code
                VStack(alignment: .leading) {
                    HStack {
                        Text(user?.description ?? "")
                            .font(.subheadline)
                            .foregroundColor(appTheme.colorScheme == .dark ? .white : .black)
                        Spacer()
                        Button(action: {
                            showEditAlert = true
                        }) {
                            FontIcon.text(.materialIcon(code: .edit), fontsize: 25, color: .gray)
                        }
                    }
                    .alert("Changer de description", isPresented: $showEditAlert) {
                        TextField("Description", text: $editedDescription)
                            .onTapGesture {
                                UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
                            }
                        Button("OK", role: .cancel) {
                            Task {
                                do {
                                    print(editedDescription)
                                    let desc = UserInfo(description: editedDescription)
                                    try await self.updateProfil.updateProfil(userInfo: desc)
                                } catch {
                                    
                                }
                            }
                        }
                    }
                    // rest of code
                }
            }
        }
    }
}

I've tried absolutely everything, closing the keyboard, changing the redirection, etc., but I don't know what's causing the problem. Do you know why when I click on the textfield, the application "crashes" and redirects me to my login page when the keyboard opens?

  • 1
    Welcome to Stack Overflow! Please take the [tour](https://stackoverflow.com/tour) and see: [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Reproducible Example (MRE)](https://stackoverflow.com/help/minimal-reproducible-example). There is too much irrelevant code here, and not enough code to build and run for testing. Cut this down to bare bones that still has the issue. I can see you tried some, but more needs to be cut. Whatever code you come up with, place in a new project and run it before posting it. – Yrb Jun 16 '23 at 14:48

0 Answers0