1

Removing an item from the end of the pages array results in an error when swiping through the pages. Is there a way to dynamically remove views from TabView as needed?

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")
                }
            }
        }
    }
}
Hamer
  • 1,354
  • 1
  • 21
  • 34

0 Answers0