5

I have setup a UI in which there are 6 fields in the ScrollView as the following:

...
         ScrollView {

                IndicatorTextField(
                    index: 0,
                    hint: "Full Name",
                    icon: .imageUserBlue,
                    activeFieldTag: self.$activeFieldTag,
                    type: .alphabet
                ).padding(.top, 48)

                IndicatorTextField(
                    index: 1,
                    hint: "Email Address",
                    icon: .imageEmail,
                    activeFieldTag: self.$activeFieldTag,
                    type: .emailAddress
                ).padding(.top, 8)

                IndicatorTextField(
                    index: 2,
                    hint: "Home Town",
                    icon: .imageLocation,
                    activeFieldTag: self.$activeFieldTag,
                    type: .alphabet
                ).padding(.top, 8)

                IndicatorTextField(
                    index: 3,
                    hint: "Password",
                    icon: .imageLock,
                    activeFieldTag: self.$activeFieldTag,
                    indicatorColor: .reddishPink
                ).padding(.top, 8)

                IndicatorTextField(
                    index: 4,
                    hint: "Date of Birth",
                    icon: .imageCalender,
                    activeFieldTag: self.$activeFieldTag,
                    type: .numbersAndPunctuation,
                    indicatorColor: .reddishPink
                ).padding(.top, 8)

                IndicatorTextField(
                    index: 5,
                    hint: "Gender",
                    icon: .imageUserRed,
                    activeFieldTag: self.$activeFieldTag,
                    indicatorColor: .reddishPink,
                    returnKey: .default
                ).padding(.top, 8)

                Spacer()
                    .frame(width: 0, height: -keyboardObserver.lastViewAdjustment)
            }
            .background(GeometryGetter(rect: $keyboardObserver.lastViewRect))
...

Here is keyboardObserver

When the keyboard appears then I just change the Spacer height enough to scroll till last field.

Now, I want to auto scroll the firstResponder above the Keyboard.

Does anyone know about how to scroll ScrollView with SwiftUI code?

1 Answer 1

1

I haven't found a way to scroll ScrollView in SwiftUI so far, but I used a workaround, where I tell the parent scroll view to scroll from a wrapper for UITextField: https://lostmoa.com/blog/ScrollTextFieldIntoVisibleRange/

I found it's an easier solution than wrapping UIScrollView.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.