Skip to content

When a TextField is focused, SwiftUI initializes the View #1

@jeongHunE

Description

@jeongHunE

Description(Problem)

When a TextField is focused, SwiftUI initializes the signUpView every time

Code

An example is below

struct SignUpView<SVM: SignUpVM>: View {
    @EnvironmentObject private var signUpViewModel: SVM
    @State private var idInput: String = ""
    
    var body: some View {
        ScrollView {
            VStack(spacing: 25) {
                //MARK: - Default Input
                IdInputView<SVM>(idInput: $idInput)
                    .environmentObject(signUpViewModel)
            }
        }
    }
}

struct IdInputView<SVM: SignUpVM>: View {
    @EnvironmentObject private var signUpViewModel: SVM
    @Binding private var idInput: String
    
    var body: some View {
        VStack {
            SignUpInputView(text: $idInput,
                            inputState: Binding(
                                get: {
                                    self.signUpViewModel.defaultStates["id"] ?? .isInitial
                                }, set: { newValue in
                                    self.signUpViewModel.defaultStates["id"] = newValue
                                }
                            ),
                            header: "아이디", placeholder: "학번 또는 교직원 번호를 입력해 주세요", keyboardType: .numberPad)
        }
    }
}

struct SignUpInputView: View {
    @Binding private var text: String
    @Binding private var inputState: InputState
    
    var body: some View {
        VStack {
            HStack {
                Text(header).bold().font(.subheadline).foregroundColor(colorScheme == .light ? .black : .white)
                
                Spacer()
            }
            .offset(x: 16)
            
            RoundedRectangle(cornerRadius: 30)
                .fill(CustomColor.getSignUpInputGray(colorScheme))
                .frame(height: UIScreen.main.bounds.width * 0.13)
                .overlay {
                    switch style {
                    case .normal:
                        TextField(placeholder, text: $text)
                            .keyboardType(keyboardType)
                            .autocorrectionDisabled()
                            .padding()

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions