Ios 如何滚动滚动视图,使文本字段在SwiftUI中移动到键盘上方?
我已经设置了一个UI,其中Ios 如何滚动滚动视图,使文本字段在SwiftUI中移动到键盘上方?,ios,swift,swiftui,swiftui-list,Ios,Swift,Swiftui,Swiftui List,我已经设置了一个UI,其中滚动视图中有6个字段,如下所示: ... ScrollView { IndicatorTextField( index: 0, hint: "Full Name", icon: .imageUserBlue, activeFieldTag: self.$acti
滚动视图中有6个字段,如下所示:
...
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))
...
这是
当键盘出现时,我只需更改间隔符的高度,以滚动到最后一个字段
现在,我想自动滚动键盘上方的firstResponder
有人知道如何使用SwiftUI代码滚动ScrollView
吗?到目前为止,我还没有找到在SwiftUI中滚动ScrollView
的方法,但我使用了一种变通方法,我告诉父滚动视图从UITextField
的包装中滚动:
我发现这是一个比包装UIScrollView
更简单的解决方案