0

Using these code

struct SwiftUIView: View {
    struct BodySizePreferenceKey: PreferenceKey {
        static var defaultValue: CGRect { .zero }
        static func reduce(value: inout CGRect, nextValue: () -> CGRect) {}
    }
    
    var body: some View {
        ZStack {
            Color.blue.ignoresSafeArea()
                .background(
                    GeometryReader { geometry in
                        Spacer()
                            .preference(
                                key: BodySizePreferenceKey.self,
                                value: geometry.frame(in: .global)
                            )
                    }
                )
                .onPreferenceChange(BodySizePreferenceKey.self) {
                    print(">>><<< \($0)")
                }
        }
    }
}

And running it on iPad, you can see the preference size changing when the app has gone to background (user minimize app). It seems that the size will change to rotated orientation and then back to the original orientation.

>>><<< (0.0, 24.0, 1024.0, 1322.0) --> on first appear
>>><<< (0.0, 24.0, 1366.0, 980.0)  --> after the app is in background (presses home)
>>><<< (0.0, 24.0, 1024.0, 1322.0) --> less than a second after the previous change

It happens only on iPad (iOS 14 and 16, not 15) and not on iPhone as far as I know. Is this a bug from SwiftUI or am I using it incorrectly?

Oscar Yuandinata
  • 1,025
  • 1
  • 13
  • 31

0 Answers0