Reputation: 1573
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?
Upvotes: 5
Views: 1283
Reputation: 1184
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
.
Upvotes: 1