iOS14.2中的SwiftUI PageTabView将多次调用ChildView onAppear方法 [英] SwiftUI PageTabView in iOS14.2 will Recall ChildView onAppear method Many times
问题描述
我在SwiftUI中使用TabView PageTabViewStyle来显示网页视图,当我滑动此TabView时,我发现子视图将调用onAppear方法很多次,有人可以告诉我为什么吗?
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?
这是我的代码
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 correct case is to print only once per swipe
它在ios14.2中只是有问题,14.1可以,您可以在Github中加载我的代码: https://github.com/werbhelius/TabViewBug
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版本:12.1(12A7403)
Xcode version: 12.1 (12A7403)
设备:iPhone 6s iOS 14.2
Device: iPhone 6s iOS 14.2
我认为您可以在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
推荐答案
View
s是由SwiftUI决定预先加载的.有时要比其他设备更多,具体取决于设备的可用资源.即使onAppear
出现在视线之外(已预加载),也会被调用
View
s are preloaded at the discretion of SwiftUI. Sometimes more than others depending on the device's available resources. onAppear
is called even if it has appeared out of view (pre-loaded)
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()
}
}
这篇关于iOS14.2中的SwiftUI PageTabView将多次调用ChildView onAppear方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!