gpt4 book ai didi

swiftui - 当键盘出现时滚动到 SwiftUI Scrollview 的最底部

转载 作者:行者123 更新时间:2023-12-05 02:01:46 42 4
gpt4 key购买 nike

在我的 SwiftUI 项目中,我有以下布局

ScrollView {
VStack {
Text("some text that may change while editing the stuff below")
TextField(...)
TextField(...)
Button(...)
}
}

我想要实现的是,只要 TextField 获得焦点,ScrollView 就会滚动到最底部,即按钮可见。我尝试使用包装的 UIScrollView 和 keyboardWillShow-notifications,但没有找到适用于 iOS 13 和 14 的任何内容。

实现此目标的最佳方法是什么?

最佳答案

您可以使用 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)
}
}
}

关于swiftui - 当键盘出现时滚动到 SwiftUI Scrollview 的最底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66313827/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com