2023-02-27

how can I keep the keyboard opened with @FocusState with SwiftUI without a bounce?

I am trying to develop a view where the user must input his name and surname. these two textfields have a FocusState. Everything is working well less a little bounce when the focus changes his goal. bounce app

I do not know what is happening in my code for this issue. this is my code:

struct OnboardingViewPart2: View {

enum Field: Hashable{
    case name
    case surname
}

@State var displayName         = ""
@State var showImagePicker     = false
@State var isSomePhotoSelected = false
@State var displaySurname      = ""
@FocusState var focusedField : Field?


// For image picker
@State var imageSelected: UIImage = UIImage(systemName: "person.fill")!
@State var sourceType: UIImagePickerController.SourceType = .photoLibrary

var body: some View {
    
    VStack(alignment: .center, spacing: 20, content: {
        

//other code

        // MARK: Textfield group
        Group{
            
            TextField("Add your name here...", text: $displayName)
                .padding()
                .frame(height: 60)
                .frame(maxWidth: .infinity)
                .background(Color.MyTheme.beige)
                .cornerRadius(12)
                .font(.headline)
                .autocapitalization(.sentences)
                .padding(.horizontal)
                .focused($focusedField, equals: .name)
            
            TextField("Add your surname here...", text: $displaySurname)
                .padding()
                .frame(height: 60)
                .frame(maxWidth: .infinity)
                .background(Color.MyTheme.beige)
                .cornerRadius(12)
                .font(.headline)
                .autocapitalization(.sentences)
                .padding(.horizontal)
                .focused($focusedField, equals: .surname)
        }
        .onSubmit {focusedField = focusedField == .name ? .surname : nil}
        
        //other code
        
    }) // VStack
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .background(Color.MyTheme.purple)
    //.edgesIgnoringSafeArea(.all)
    .sheet(isPresented: $showImagePicker, content: {
        ImagePicker(imageSelected: $imageSelected, sourceType: $sourceType, isSomeImageSelected: $isSomePhotoSelected)
    }) // Picker
    
    
}

}

// Updated!!

after updating the code with the proposed solution this is the new result: new result



No comments:

Post a Comment