SwiftUI TabView Rearrange Tab Order Preservation
Asked Answered
P

0

8

It looks like Apple's TabView supports rearrangements automatically, but it quickly loses a rearrangement after you click on a tab.

Here's my playground code.

import SwiftUI
import PlaygroundSupport

struct ContentView: View {
    @State var arr = ["1","2","3","4","5","6"]
    var body: some View {
        TabView {
            ForEach(self.arr, id: \.self) { name in
                Text("Tab \(name) content")
                    .tabItem {
                        Image(systemName: "\(name).square.fill")
                        Text(name)
                    }
            }
        }
        .font(.headline)
    }
}

PlaygroundPage.current.setLiveView(ContentView())

Here's the steps I go through in the gif.

  1. I start on tab "1"
  2. I go to the "more" tab and then click "edit"
  3. I swap tab "2" with tab "3"
  4. Close the rearrange screen
  5. Then when I click tab "2" (which is now in position 3), the app resets the tabs back to their original order!

Demo of issue

I'm not sure if this is a SwiftUI bug or if there's a onRearrange hook I should have been listening to or what.

Peeved answered 9/12, 2019 at 19:29 Comment(6)
Theoretically .onMove should be working, as it is dynamic content, but it does not - tested on Xcode 11.2 ... it looks like feature uncompleted ))Copious
I’m thinking so as wellPeeved
As of XC 11.4.1, this misbehaviour it is still there. Is there an event to catch and possible to read the new order so I can change the original order array?Scabies
Its 2022 and as of Xcode 14 Beta 5, this is still completely broken...Cly
Does anybody know how to suppress the "Edit" button? I'd rather not have users encounter this.Heriot
@BobJacobsen I believe there is a library out there to hook into UIKit views from SwiftUI. Maybe that would be a way to do itPeeved

© 2022 - 2024 — McMap. All rights reserved.