从 TabView 中动态移除标签页,PageTabViewStyle

时间:2021-02-23 05:16:47

标签: ios swift swiftui swiftui-tabview

从 pages 数组的末尾删除项目会导致在浏览页面时出错。有没有办法根据需要从 TabView 动态删除视图?

import SwiftUI

struct ContentView: View {
    @State var pages = ["1", "2", "3", "4"]
    
    var body: some View {
        NavigationView {
            TabView {
                ForEach(pages, id: \.self) { page in
                    Text(page)
                }
            }
            .tabViewStyle(PageTabViewStyle())
            .toolbar {
                Button(action: {
                    pages.removeLast()
                }) {
                    Image(systemName: "trash")
                }
            }
        }
    }
}

0 个答案:

没有答案