0

How to get the screen width and height only in landscape orientation , i have two ipad 4 tablets and on one of them it takes me the landscape width & height , and on the other one it takes me the portrait one even if it's orientation is on lanscape

at the moment i am using this but its not working well

 CGFloat width = self.view.bounds.size.width;
 CGFloat height = self.view.bounds.size.height;
Alex Bonta
  • 101
  • 4
  • 15
  • 1
    Your title doesn't match what the question is actually asking, in your title you say only in portrait and in the question you say only in landscape. Which one is it? Please amend your question and/or title so it reflects what you want. – Popeye Nov 04 '14 at 10:49
  • See this question: http://stackoverflow.com/questions/7905432/how-to-get-orientation-dependent-height-and-width-of-the-screen iOS 8 changed the behavior a little – Sebastian Wramba Nov 04 '14 at 10:49
  • Quick & dirty fix would be to take the larger of the two and call it "width", as "landscape" is the orientation we call when the width is larger than the height.. – rdurand Nov 04 '14 at 10:58

2 Answers2

3

Use [UIScreen mainScreen].bounds you'll have the same.

Discussion
This rectangle is specified in the current coordinate space, which takes into account any interface rotations in effect for the device. Therefore, the value of this property may change when the device rotates between portrait and landscape orientations.

Use [UIScreen mainScreen].nativeBounds in iOS8 only to get the portrait-locked bounds.

Discussion
This rectangle is based on the device in a portrait-up orientation. This value does not change as the device rotates.

David Ansermot
  • 6,052
  • 8
  • 47
  • 82
3

Swift 3

    let pixelWidth = UIScreen.main.nativeBounds.width
    let pixelHeight = UIScreen.main.nativeBounds.height
    let pointWidth = pixelWidth / UIScreen.main.nativeScale
    let pointHeight = pixelHeight / UIScreen.main.nativeScale

    print ("Pixels: \(pixelWidth) x \(pixelHeight)")
    print ("Points: \(pointWidth) x \(pointHeight)")

On a 6s Plus will print...

Pixels: 1080.0 x 1920.0
Points: 414.0 x 736.0
AlBeebe
  • 8,101
  • 3
  • 51
  • 65