6
struct ContentView: View {
    
    @State private var selectedIdx = 0
    
    var body: some View {
        TabView(selection: $selectedIdx) {
            ForEach(0..<5) { idx in
                Text("\(idx)")
            }
        }
        .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
    }
}

Environment: Xcode 12.2 iOS 14.2

TabView in SwiftUI memory continuously increases as I swipe between pages. Running instruments, I do not see any leaks but the allocation and persistent memory increases continuously.

Ideally, even if the pages are being recreated every time, the total memory consumed by the 5 pages (as in the code above) should not change.

Is this a bug in SwiftUI? Or am I missing something?

malhal
  • 26,330
  • 7
  • 115
  • 133
dushandz
  • 117
  • 5
  • Am facing the same issue and haven't been able to figure out what's wrong. It looks like a bug with `TabView`. When your pages have complex UI, it causes bad performance issues as you swipe between pages. Have you been able to figure out any fix? – akshaynhegde Dec 25 '20 at 15:27
  • NO, i still don't konw why – dushandz Jan 19 '21 at 07:16
  • Have you found the solution for this? I'm facing this too – phuongzzz Jul 25 '22 at 07:53

1 Answers1

0

This code fixes a bug for me

struct TabViewWrapper<Content: View, Selection: Hashable>: View {
    @Binding var selection: Selection
    @ViewBuilder let content: () -> Content
    
    var body: some View {
        TabView(selection: $selection, content: content)
    }
}

Replace TabView(selection:) to TabViewWrapper(selection:)

TabViewWrapper(selection: $selection) {
    tabContent
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
stalkermv
  • 61
  • 4