I use TabView PageTabViewStyle with SwiftUI to display a pageview, when I swipe this TabView I find child view will Recall onAppear method Many times, Can someone tell me why?
This is my code
import SwiftUI
struct Pageview: View {
@StateObject var vm = PageViewModel()
var body: some View {
VStack {
DragViewBar().padding(.top, 14)
TabView(selection: $vm.selectTabIndex) {
TextView(index: "0").tag(0)
TextView(index: "1").tag(1)
TextView(index: "2").tag(2)
TextView(index: "3").tag(3)
TextView(index: "4").tag(4)
TextView(index: "5").tag(5)
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
}
}
}
struct TextView: View {
let index: String
var body: some View {
VStack {
Text(index)
}
.onAppear { print(index) }
}
}
struct DragViewBar: View {
var body: some View {
Rectangle()
.frame(width:36.0,height:5.0).foregroundColor(Color.blue)
.cornerRadius(100)
}
}
class PageViewModel: ObservableObject {
@Published var selectTabIndex = 0
}
The result of the console printing
The correct case is to print only once per swipe
It just has a problem in ios14.2, 14.1 will be ok, you can load my code in Github: https://github.com/werbhelius/TabViewBug
Xcode version: 12.1 (12A7403)
Device: iPhone 6s iOS 14.2
I think you can reproduce this problem on any device in iOS 14.2
I look forward to your help to solve this problem. Thank you