How can one get the dimensions of the screen in iOS?
Currently, I use:
lCurrentWidth = self.view.frame.size.width; lCurrentHeight = self.view.frame.size.height;
in viewWillAppear:
and willAnimateRotationToInterfaceOrientation:duration:
The first time I get the entire screen size. The second time i get the screen minus the nav bar.
The size of a 16:9 screen depends on how long the screen's diagonal is, as 16:9 is merely the ratio of the screens width to its height. If you have the screens diagonal, you can multiply that measurement by 0.872 to get the screen's width. You can also multiply the diagonal by 0.49 to get the screen's height.
You can get the device screen width via the screen. width property. Sometimes it's also useful to use window. innerWidth (not typically found on mobile devices) instead of screen width when dealing with desktop browsers where the window size is often less than the device screen size.
How can one get the dimensions of the screen in iOS?
The problem with the code that you posted is that you're counting on the view size to match that of the screen, and as you've seen that's not always the case. If you need the screen size, you should look at the object that represents the screen itself, like this:
CGRect screenRect = [[UIScreen mainScreen] bounds]; CGFloat screenWidth = screenRect.size.width; CGFloat screenHeight = screenRect.size.height;
Update for split view: In comments, Dmitry asked:
How can I get the size of the screen in the split view?
The code given above reports the size of the screen, even in split screen mode. When you use split screen mode, your app's window changes. If the code above doesn't give you the information you expect, then like the OP, you're looking at the wrong object. In this case, though, you should look at the window instead of the screen, like this:
CGRect windowRect = self.view.window.frame; CGFloat windowWidth = windowRect.size.width; CGFloat windowHeight = windowRect.size.height;
let screenRect = UIScreen.main.bounds let screenWidth = screenRect.size.width let screenHeight = screenRect.size.height // split screen let windowRect = self.view.window?.frame let windowWidth = windowRect?.size.width let windowHeight = windowRect?.size.height
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With