我使用 TabView PageTabViewStyle 和 SwiftUI 来显示页面视图,当我滑动这个 TabView 时,我发现子视图会多次调用 onAppear 方法,有人可以告诉我为什么吗?
这是我的代码
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
![enter image description here](https://i.stack.imgur.com/7bgsG.png)
正确的情况是每次滑动仅打印一次
只是在ios14.2上有问题,14.1就可以了,你可以在Github上加载我的代码:https://github.com/werbhelius/TabViewBug https://github.com/werbhelius/TabViewBug
Xcode 版本:12.1 (12A7403)
设备: iPhone 6s iOS 14.2
我认为您可以在 iOS 14.2 中的任何设备上重现此问题
我期待您的帮助来解决这个问题。谢谢
View
由 SwiftUI 自行决定预加载。有时比其他更多,具体取决于设备的可用资源。onAppear
即使它出现在视图之外(预加载)也会被调用
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)
}
//This lets you perform an operation when the value has changed
.onReceive(vm.$selectTabIndex, perform: { idx in
print("PageView :: body :: onReceive" + idx.description)
})
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
}
}
}
struct TextView: View {
let index: String
var body: some View {
VStack {
Text(index)
}
//Views are pre-loaded at the discretion of SwiftUI
.onAppear { print(index) }
.onReceive(index.publisher, perform: { idx in
print("TextView :: body :: onReceive" + idx.description)
})
}
}
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
}
struct PageView_Previews: PreviewProvider {
static var previews: some View {
PageView()
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)