SwiftUI:分段控制滚动
我正在使用分段控件在选项卡之间导航。 我的问题是,当我在一个选项卡内滚动时,所有其他选项卡也在滚动。这是我不想要的。
我希望滚动仅在所选选项卡内起作用,当我单击其他选项卡时,它会显示所选选项卡内容的顶部。
检查附加视频:https://i.sstatic.net/uBZAK.jpg
这是我的代码
谢谢!
import SwiftUI
struct ContentView: View {
@State var selectedState = 0
@State var showTopTabBar: Bool = false
var body: some View {
ZStack (alignment: . top){
ScrollView (showsIndicators: false){
Text("Welcome")
.frame(height: 300)
Picker("Options", selection: $selectedState) {
Text("First").tag(0)
Text("Second").tag(1)
Text("Third").tag(2)
}
.pickerStyle(SegmentedPickerStyle())
.background(Color.white)
.opacity(showTopTabBar ? 0 : 1)
.readPos { rect in
if showTopTabBar != (rect.minY <= 100) {
showTopTabBar.toggle()
}
}
if (selectedState==0) {
FirstView()
}
else if (selectedState==1){
Secondview()
}
else {
ThirdView()
}
Spacer()
}
.padding()
Picker("Options", selection: $selectedState) {
Text("First").tag(0)
Text("Second").tag(1)
Text("Third").tag(2)
}
.pickerStyle(SegmentedPickerStyle())
.padding()
.background(Color.white)
.opacity(showTopTabBar ? 1 : 0)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
extension View {
func readPos(onChange: @escaping (CGRect) -> Void) -> some View {
background(
GeometryReader { geometryProxy in
Color.clear
.preference(key: CoordinatesPreferenceKey.self, value: geometryProxy.frame(in: .global))
}
)
.onPreferenceChange(CoordinatesPreferenceKey.self, perform: onChange)
}
}
private struct CoordinatesPreferenceKey: PreferenceKey {
static var defaultValue: CGRect = .zero
static func reduce(value: inout CGRect, nextValue: () -> CGRect) {}
}
I'm using a segmented control to navigate between tabs.
My issue is, when I scroll inside one tab, all the other tabs are also strolling. Which is something I don't want.
I want that the scrolling works only inside the selected tab and when I click on the other tabs, it shows the top of the content of the selected tab.
Check attached video : https://i.sstatic.net/uBZAK.jpg
and here is my code
Thanks!
import SwiftUI
struct ContentView: View {
@State var selectedState = 0
@State var showTopTabBar: Bool = false
var body: some View {
ZStack (alignment: . top){
ScrollView (showsIndicators: false){
Text("Welcome")
.frame(height: 300)
Picker("Options", selection: $selectedState) {
Text("First").tag(0)
Text("Second").tag(1)
Text("Third").tag(2)
}
.pickerStyle(SegmentedPickerStyle())
.background(Color.white)
.opacity(showTopTabBar ? 0 : 1)
.readPos { rect in
if showTopTabBar != (rect.minY <= 100) {
showTopTabBar.toggle()
}
}
if (selectedState==0) {
FirstView()
}
else if (selectedState==1){
Secondview()
}
else {
ThirdView()
}
Spacer()
}
.padding()
Picker("Options", selection: $selectedState) {
Text("First").tag(0)
Text("Second").tag(1)
Text("Third").tag(2)
}
.pickerStyle(SegmentedPickerStyle())
.padding()
.background(Color.white)
.opacity(showTopTabBar ? 1 : 0)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
extension View {
func readPos(onChange: @escaping (CGRect) -> Void) -> some View {
background(
GeometryReader { geometryProxy in
Color.clear
.preference(key: CoordinatesPreferenceKey.self, value: geometryProxy.frame(in: .global))
}
)
.onPreferenceChange(CoordinatesPreferenceKey.self, perform: onChange)
}
}
private struct CoordinatesPreferenceKey: PreferenceKey {
static var defaultValue: CGRect = .zero
static func reduce(value: inout CGRect, nextValue: () -> CGRect) {}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
ScrollViewReader
向上滚动以在选项卡更改时开始:现在始终在选项卡更改时向上滚动。如果您想保留每个选项卡中的位置,则必须根据显示内容的
.id
将它们保存在 @State 变量中。You can use
ScrollViewReader
to scroll up to start on change of Tabs:This now always scrolls up on change of Tab. If you want to preserve the positions in each tab you have to save them in @State vars based on
.id
s of your displayed content.