I have a universal iPad / iPhone application which presents different interface for .regular and .compact horizontal size classes. The architecture of the app is similar to this refined example:
import SwiftUI
struct CompactView: View {
var body: some View {
Text("CompactView")
}
}
struct ContentView: View {
@Environment(\.horizontalSizeClass) var horizontalSizeClass
var body: some View {
Group {
if horizontalSizeClass == .regular {
NavigationSplitView {
Text("Sidebar")
} detail: {
Text("Detail")
}
} else {
CompactView()
.onAppear {
print("Why am I here?")
}
}
}
.onChange(of: horizontalSizeClass) { newValue in
print("Size class: \(newValue!), app state: \(UIApplication.shared.applicationState)")
}
}
}
Recently I've discovered a strange behaviour: when user presses home button on iPad, application interface changes its horizontalSizeClass from .regular to .compact and back to .regular for a moment when entering background state. And it's sufficient to lose all transient state including presented modals with their state.
The console output for this example in described case is:
Size class: compact, app state: UIApplicationState(rawValue: 2)
Why am I here?
Size class: regular, app state: UIApplicationState(rawValue: 2)
Could anybody explain why this is happening or give any relevant reference?
Yes, I'm aware of the split mode and multitasking and that iPad interface transitions to .compact in those cases. But my question relates to only this one specific case: is this an expected behaviour (and if yes, then why)? Or is it an SDK bug? Thanks.
Yes, I'm aware of the split mode and multitasking and that iPad interface transitions to .compact in those cases.
This is related. When an app enters the background, the system takes snapshots of the app in multiple configurations, such as the sizes used when your app is displayed alongside others in various multitasking configurations. These snapshots are then displayed in places like when you display the open apps and their scenes to configure multi-tasking, or go to switch apps. This is an expected behavior, and not a bug.