FB13103224: SwiftUI.TabView's tab useless onAppear call occurs when TabView is removed
yimajo opened this issue Β· comments
Yoshinori Imajo commented
- Date: 2023-09-02
- Resolution: Open
- Area: SwiftUI
- OS: iOS 17 beta 8
- Type: incorrect/Unexpected Behavior
- Keywords:
Details
Description
When SwiftUI.TabView is removed from the View, each Tab unnecessarily calls .onAppear
.
Condition. There must be multiple tabs, and each of the multiple tabs must be visible at least once. Specifically, this occurs when the Home screen is displayed in the TabView, and the tab is destroyed by logging out after switching to the profile screen.
import SwiftUI
struct ContentView: View {
@State var loggedIn: Bool = false
var body: some View {
switch loggedIn {
case false:
Button("Login") {
loggedIn = true
}
case true:
TabView {
NavigationView {
Text("Home")
.navigationBarTitle("Home")
}
.tabItem {
Image(systemName: "house")
Text("Home")
}
.onAppear {
print("π Home on appear π’π’π’π’")
}
.onDisappear {
print("π Home on disappear")
}
NavigationView {
Text("Profile")
.navigationBarTitle("Profile")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button("Logout") {
loggedIn = false
}
}
}
}
.tabItem {
Image(systemName: "person")
Text("Profile")
}
.onAppear {
print("π Profile on appear")
}
.onDisappear {
print("π Profile on disappear")
}
}
}
}
}
Actual logs...
π Home on appear π’π’π’π’
π Profile on appear
π Home on disappear
π Home on appear π’π’π’π’
π Home on disappear
π Profile on disappear
Expected Logs...
π Home on appear π’π’π’π’
π Profile on appear
π Home on disappear
π Profile on disappear
This is true not only for onAppear
but also for task
.
Please refer to the Developer Forum for more information.
https://developer.apple.com/forums/thread/719521?login=true