[英]How can I fix - Swiftui tvOS focus error on tvOS 15.2
在tvOS 15.2中,在滾動中使用 tabView 時,從左向右移動時它會返回。 雖然tvOS 14 和 16沒有這個問題,但我在 15.2 中遇到了這個問題。
可以看到動圖: https://gifyu.com/image/SmE5Q
我的代碼
import SwiftUI
struct ContentView: View {
@State var index: Int = 0
var body: some View {
ScrollView(showsIndicators: true) {
VStack {
TabView(selection: $index) {
ForEach(0..<5, id: \.self) { item in
//PageContent(model: data[item])
Button {
} label: {
Text("METIN ATALAY \(item)")
}
}
}
.background(Color.yellow)
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .always))
.padding(.bottom, 0)
.frame(width: UI.screenSize.width, height: UI.screenSize.height * 0.64)
Button {
} label: {
Text("\(UUID().uuidString)")
}
Button {
} label: {
Text("\(UUID().uuidString)")
}
}
}
}
}
struct UI {
static let screenBounds: CGRect = UIScreen.main.bounds
static let screenSize: CGSize = CGSize(width: screenBounds.width, height: screenBounds.height)
}
下面的解決方案沒有使用TabView
,而是解決了我的問題
VStack {
ScrollView(.horizontal) {
ScrollViewReader { proxy in
HStack(spacing: 0) {
ForEach(0..<data.count, id: \.self) { index in
PageContent(model: data[index])
.id(index)
}
}
.onChange(of: pageIndex) { newValue in
withAnimation {
proxy.scrollTo(newValue, anchor: .leading)
}
}
}
}.padding(.bottom)
.frame(width: deviceSize.width, height: layout.getBannerHeight(by: deviceSize))
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.