【发布时间】:2021-11-16 16:19:33
【问题描述】:
是否可以制作包装其内容的分页TabView?我不知道内容的高度(它是一个调整大小以适合屏幕宽度的图像)所以我不能使用frame 修饰符。
我的代码如下所示:
ZStack(alignment: .topTrailing) {
TabView {
ForEach(data) { entry in
VStack {
entry.image
.resizable()
.scaledToFit()
.frame(maxWidth: .infinity)
Text(entry.description)
}
}
}
.tabViewStyle(.page(indexDisplayMode: .never))
Color.red
.frame(width: 20, height: 20)
}
问题是TabView与屏幕一样大,PageIndicator被放置在屏幕的右上角而不是图像的右上角。坦克的任何建议。
编辑:
我添加了可重现的代码。 PageIndicator 被红色矩形取代。
struct Test: View {
struct Entry: Identifiable {
let image: Image
let description: String
var id: String { description }
}
let data = [
Entry(image: Image(systemName: "scribble"), description: "image 1"),
Entry(image: Image(systemName: "trash"), description: "image 2")
]
var body: some View {
ZStack(alignment: .topTrailing) {
TabView {
ForEach(data) { entry in
VStack {
entry.image
.resizable()
.scaledToFit()
.frame(maxWidth: .infinity)
Text(entry.description)
}
}
}
.tabViewStyle(.page(indexDisplayMode: .never))
Color.red
.frame(width: 20, height: 20)
}
}
}
【问题讨论】: