IPad - getting screen size in portrait and landscape orientation

I use the following code to get the width of the screen:

CGFloat width = [UIScreen mainScreen].bounds.size.width - 100; 

But its width is β€œ668.0” both in the portrait and in the landscape. How can I get a different width depending on the orientation of the device.

+4
source share
3 answers

I ran into the same problem as you, and all I could find was to check the orientation and calculate the height and width, as shown below:

 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; screenHeight = orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight ? 748 : 1004; screenWidth = orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight ? 1024 : 768; 

These sizes are for the obvioulsy iPad, but it can work on the iPhone with the correct sizes.

Personally, I think he is a little poor, but I could not find anything else.

+3
source

Ok In this case, we can use

  CGRect viewFrame = self.view.frame;
 int width = viewFrame.size.width;
 int height = viewFrame.size.height; 
+3
source
  CGSize winSize = [[CCDirector sharedDirector] winSize]; 

winSize.width will give you the width and winSize.height will give you the height.

+1
source

Source: https://habr.com/ru/post/949299/


All Articles