SwiftUI preview device screen size differs from simulator

I use the screen size for some SwiftUI view layouts to adapt to various screen sizes. I have found that for some devices, such as the iPhone 13 mini, the screen size returned from UIScreen.main.bounds differs from the preview device and the simulator. Is this expected behavior?

Answered by Developer Tools Engineer in 714019022

Yes this can be expected. It can also happen in scenarios for normal app flows on-device, so this isn't specific to previews. Instead of using UIScreen.main.bounds, you should use a GeometryReader on your view to measure the space you are actually being allocated, or to use size classes to detect the semantic "size" of the space your view is ultimately allocated.

Accepted Answer

Yes this can be expected. It can also happen in scenarios for normal app flows on-device, so this isn't specific to previews. Instead of using UIScreen.main.bounds, you should use a GeometryReader on your view to measure the space you are actually being allocated, or to use size classes to detect the semantic "size" of the space your view is ultimately allocated.

SwiftUI preview device screen size differs from simulator
 
 
Q