anuj
anuj

Reputation: 1060

Get screen size on iOS 8

I tried [[UIScreen mainScreen] bounds] to get the screen size of a resizable iPhone on Xcode 6 simulator. But it doesn't take into account the new screen width and screen height I assign to it.

Is there a better way to get the screen size? Even if I set the screen size to 320x800 the [[UIScreen mainScreen] bounds] returns me (CGRect) $0 = origin=(x=0, y=0) size=(width=768, height=1024)

enter image description here

Upvotes: 10

Views: 14377

Answers (3)

Kiryl Bielašeŭski
Kiryl Bielašeŭski

Reputation: 2683

In Swift 4.0, 3.0

let screenRect:CGRect = UIScreen.main.bounds

Upvotes: 2

Esqarrouth
Esqarrouth

Reputation: 39181

This gets in iOS8 and iOS9. Not sure about 7 haven't tested:

var screenWidth: CGFloat {
    if UIInterfaceOrientationIsPortrait(screenOrientation) {
        return UIScreen.mainScreen().bounds.size.width
    } else {
        return UIScreen.mainScreen().bounds.size.height
    }
}
var screenHeight: CGFloat {
    if UIInterfaceOrientationIsPortrait(screenOrientation) {
        return UIScreen.mainScreen().bounds.size.height
    } else {
        return UIScreen.mainScreen().bounds.size.width
    }
}
var screenOrientation: UIInterfaceOrientation {
    return UIApplication.sharedApplication().statusBarOrientation
}

These are included as a standard function in:

https://github.com/goktugyil/EZSwiftExtensions

Upvotes: 1

Yogesh More
Yogesh More

Reputation: 239

Do following

CGRect Rect=[[UIScreen mainScreen] bounds];

So it will preserves the bounds in the rect. try once.

Upvotes: 17

Related Questions