swa*_*ner 6 scrollview swiftui
在我的 SwiftUI 项目中,我有以下布局
ScrollView {
VStack {
Text("some text that may change while editing the stuff below")
TextField(...)
TextField(...)
Button(...)
}
}
Run Code Online (Sandbox Code Playgroud)
我想要实现的是,每当 TextField 获得焦点时,ScrollView 就会滚动到最底部,即按钮可见。我尝试了包装UIScrollView和键盘WillShow通知,但没有找到任何适用于iOS 13和14的东西。
实现这一目标的最佳方法是什么?
您可以通过使用 a 来实现这一点ScrollViewReader,如果您不需要onTapGesture,您可以使用keyboardWillShow-notifications
ScrollView {
ScrollViewReader { value in
VStack {
Text("some text that may change while editing the stuff below").id(0)
TextField(...)
.id(1)
.onTapGesture {
withAnimation() {
value.scrollTo(3, anchor: .top) //or if you want the button in the center use .center
}
}
TextField(...).id(2)
Button(...).id(3)
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5689 次 |
| 最近记录: |