Jac*_*cky 5 paging tabview swiftui
我正在通过 TabView 创建一个垂直分页视图
一切都很完美,除了奇怪的右边距,如下图所示。
这是我使用的代码。如果有人能指出根本原因,我将不胜感激。
import SwiftUI
fileprivate struct VCompatibleTabView<Content: View>: View {
let proxy: GeometryProxy
let content: Content
init(proxy: GeometryProxy, @ViewBuilder content: () -> Content) {
self.proxy = proxy
self.content = content()
}
var body: some View {
if #available(iOS 15.0, *) {
// Credit to Gary Tokmen for this bit of Geometry Reader code: https://blog.prototypr.io/how-to-vertical-paging-in-swiftui-f0e4afa739ba
TabView {
content
.rotationEffect(.degrees(-90)) // Rotate content
.frame(
width: proxy.size.width,
height: proxy.size.height
)
}
.frame(
width: proxy.size.height, // Height & width swap
height: proxy.size.width
)
.rotationEffect(.degrees(90), anchor: .topLeading) // Rotate TabView
.offset(x: proxy.size.width) // Offset back into screens bounds
.tabViewStyle(
PageTabViewStyle(indexDisplayMode: .never)
)
} else {
ScrollView(.vertical, showsIndicators: false) {
LazyVStack(spacing: 0) {
content
}
}
.frame(
width: proxy.size.width,
height: proxy.size.height) }
}
}
struct BBYouView: View {
var body: some View {
ZStack {
GeometryReader { proxy in
VCompatibleTabView(proxy: proxy) {
ForEach(0..<3, id: \.self) { item in
Rectangle().fill(Color.pink)
.frame(
width: proxy.size.width,
height: proxy.size.height
)
}
}
}
}
.background(Color.yellow)
}
}
Run Code Online (Sandbox Code Playgroud)
从iOS 17开始,您不需要旋转选项卡视图,您可以通过在 上应用以下修饰符来启用分页行为ScrollView:
.scrollTargetBehavior(.paging)
Run Code Online (Sandbox Code Playgroud)
因此,堆栈内的任何内容ScrollView(例如堆栈内的简单内容)都将根据需要进行分页。ForEach这适用于滚动视图horizontal和vertical滚动视图。
| 归档时间: |
|
| 查看次数: |
1983 次 |
| 最近记录: |