考虑到下面的代码,我希望我的 TabView 在显示我的选项卡 View 时显示随机计算的图像,但事实并非如此。它将始终在索引 0 处显示第一张图像。如果我在 TabView 初始值设定项(即 .constant(n)
)中对选择进行硬编码,那么它将正确显示所选图像。我已经为此提交了一份雷达 (FB7844985),但想检查一下我是否没有遗漏一些明显的东西?
struct ContentView: View {
let assets: [String] = (1...32).map { "preview_\($0)"}
@State private var selection: Int = 0
@State private var isPresented: Bool = false
var body: some View {
VStack {
Button("Random element") {
selection = Int.random(in: 1...32)
isPresented = true
}
}
.sheet(isPresented: $isPresented) {
SlideshowView(selection: $selection, assets: assets)
}
}
}
struct SlideshowView: View {
@Binding var selection: Int
var assets: [String]
var body: some View {
TabView(selection: $selection) {
ForEach(assets.indices, id: \.self) { index in
Image(assets[index])
.resizable()
.aspectRatio(contentMode: .fit)
}
}
.tabViewStyle(PageTabViewStyle())
.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
}
}
最佳答案
原因不在于模态(即在工作表中显示),而在于 TabView
不读取初始(!)选择(这绝对是一个 SwiftUI 错误)
这是解决方法(在复制代码上使用 Xcode12/iOS 14 进行测试)
struct SlideshowView: View {
@Binding var selection: Int
var assets: [String]
var body: some View {
TabView(selection: $selection) {
ForEach(assets.indices, id: \.self) { index in
Image(assets[index])
.resizable()
.aspectRatio(contentMode: .fit)
}
}
.tabViewStyle(PageTabViewStyle())
.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
.onAppear {
// WORKAROUND: simulate change of selection on appear !!
let value = selection
selection = -1
DispatchQueue.main.async {
selection = value
}
}
}
}
关于swiftui - TabView 在模态显示时不遵守选择绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62714545/