在iOS 8上获取屏幕尺寸

10

我在Xcode 6模拟器上尝试使用[[UIScreen mainScreen] bounds]来获取可调整大小的iPhone屏幕尺寸。但它没有考虑到我为其分配的新屏幕宽度和高度。

是否有更好的方法来获取屏幕尺寸?即使我将屏幕尺寸设置为320x800[[UIScreen mainScreen] bounds]也会返回(CGRect) $0 = origin=(x=0, y=0) size=(width=768, height=1024)

输入图像描述


1
在视图控制器中,你尝试过使用self.view.bounds吗? - Wyetro
3个回答

17

请执行以下步骤

CGRect Rect=[[UIScreen mainScreen] bounds];

这将在矩形中保留边界。 尝试一次。


1
你需要去掉这个*才能让它正常工作,否则会抛出错误。 - Marcel Marino
在iPhone 5上使用iOS 8.4.1时,这个似乎不起作用。它检测到的是iPhone 4/4S的屏幕尺寸。 - user2924482

2
在Swift 4.0和3.0中,
let screenRect:CGRect = UIScreen.main.bounds

1
这个问题在iOS 8和iOS 9中出现。不确定在iOS 7中是否存在,尚未测试。
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
}

这些作为标准功能包含在以下内容中:

https://github.com/goktugyil/EZSwiftExtensions


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接