[UIScreen mainScreen].bounds to use for Universal App

In order to layout some views in my app, I have:

CGRect bounds = [UIScreen mainScreen].bounds;
CGFloat currentCenter = bounds.size.width/2; 
CGFloat currentHeight = bounds.size.height; 

Now, when I run this on iPhone, iPad (and those simulators) on different generations, those values work as expected, but when I run this on "MyMac (designed for iPad)," it’s not. I assume this is supposed to work on any platform under “Universal” concept…. Or is there anything I’m missing to make it work on MyMac? 

By the way, I found the values are vary depends on the platform.

Replies

I have asked similar question before, UIScreen size looks wired if you use it in Mac: https://developer.apple.com/forums/thread/666247

In general, using UIScreen as a reference for computing your layout isn't a good idea. UIScreen represents the hardware display, and your app might only use a fractional portion of that, such as when multi-tasking on iPadOS is in use, or your app's window on macOS. Instead, your layout should size itself based on the intrinsic content size or with respect to its sibling views or the superview.


Thank you Xuelunw for your answer.
Yes, you're right.
I found UIScreen shouldn't be used for this purpose
as I asked this to Apple Developer Technical Support and gotten the answer.

Here is what it is explained:

As your app supports resizing, using the UIScreen values for layout calculations won’t work. UIScreen returns you information about the hardware display. On an iOS device before multi-tasking, these values could be a proxy for the available space to layout your interface, but with iOS multi-tasking, your app might only be allocated a fraction of that space, so the values returned from UIScreen ceased to be useful for layout purposes at that point. On macOS, that continues to be true — UIScreen is returning you information about the hardware display, which now might be as large as a desktop display, and your window can be placed at any size and at any position on the screen.

Rather than using UIScreen for layout calculations, you should look for something more local to use for the layout calculations. This might be information in a view controller, a specific view, a scene, or a window.


.... so, I changed it to the local value, then it works as expected now.