- Xcode 14.1
- Running on iPhone 14 Pro max simulator 16.1
Code...
import SwiftUI
struct ContentView: View {
@State var loggedIn: Bool = false
var body: some View {
switch loggedIn {
case false:
Button("Login") {
loggedIn = true
}
.onAppear {
print("🍏 Login on appear")
}
.onDisappear {
print("🍎 Login on disappear")
}
case true:
TabView {
NavigationView {
Text("Home")
.navigationBarTitle("Home")
.onAppear {
print("🍏 Home on appear")
}
.onDisappear {
print("🍎 Home on disappear")
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button("Logout") {
loggedIn = false
}
}
}
}
.tabItem {
Image(systemName: "house")
Text("Home")
}
NavigationView {
Text("Savings")
.navigationBarTitle("Savings")
.onAppear {
print("🍏 Savings on appear")
}
.onDisappear {
print("🍎 Savings on disappear")
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button("Logout") {
loggedIn = false
}
}
}
}
.tabItem {
Image(systemName: "dollarsign.circle")
Text("Savings")
}
NavigationView {
Text("Profile")
.navigationBarTitle("Profile")
.onAppear {
print("🍏 Profile on appear")
}
.onDisappear {
print("🍎 Profile on disappear")
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button("Logout") {
loggedIn = false
}
}
}
}
.tabItem {
Image(systemName: "person")
Text("Profile")
}
}
.onAppear {
print("🍏 Tabview on appear")
}
.onDisappear {
print("🍎 Tabview on disappear")
}
}
}
}
Video of bug... https://youtu.be/oLKjRGL2lX0
Example steps...
- Launch app
- Tap Login
- Tap Savings tab
- Tap Home tab
- Tap Logout
Expected Logs...
- 🍏 Login on appear
- 🍏 Tabview on appear
- 🍏 Home on appear
- 🍎 Login on disappear
- 🍏 Savings on appear
- 🍎 Home on disappear
- 🍏 Home on appear
- 🍎 Savings on disappear
- 🍏 Login on appear
- 🍎 Home on disappear
- 🍎 Tabview on disappear
Actual logs...
- 🍏 Login on appear
- 🍏 Tabview on appear
- 🍏 Home on appear
- 🍎 Login on disappear
- 🍏 Savings on appear
- 🍎 Home on disappear
- 🍏 Home on appear
- 🍎 Savings on disappear
- 🍏 Login on appear
- 🍏 Savings on appear
- 🍎 Home on disappear
- 🍎 Savings on disappear
- 🍎 Tabview on disappear
Error...
10 and 12 in the actual logs should not be there at all.
For each tab that you have visited (that is not the current tab) it will call onAppear
and onDisappear
for it when the tab view is removed.