ChatGPT解决这个技术问题 Extra ChatGPT

iPhone/iPad:如何以编程方式获取屏幕宽度?

嗨,我想知道是否有办法以编程方式获取宽度。

我正在寻找足以容纳 iphone 3gs、iphone 4、ipad 的通用产品。此外,宽度应根据设备是纵向还是横向(对于 ipad)而改变。

有人知道怎么做吗??我一直在寻找一段时间...谢谢!

是屏幕宽度、可用应用程序空间的宽度(没有系统栏)还是您正在寻找的特定视图的宽度?
嗯,我不确定。我想我只是想要设备的宽度(即 ipad 768 的纵向宽度和 1024 的横向宽度)但 self.view.bounds 似乎满足了这一点。看我下面的评论
请参阅考虑设备方向的this more comprehensive answer
@DrewStephens 请参阅下面的我的答案,其中考虑了方向并且只有 3 行代码:D。

P
Pang

看看UIScreen

例如。

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

如果您不希望包含状态栏(不会影响宽度),请查看 applicationFrame 属性。

更新:事实证明 UIScreen(-bounds 或 -applicationFrame)没有考虑当前的界面方向。一个更正确的方法是询问你的 UIView 的边界——假设这个 UIView 已经被它的视图控制器自动旋转。

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    CGFloat width = CGRectGetWidth(self.view.bounds);
}

如果视图没有被视图控制器自动旋转,那么您需要检查界面方向以确定视图边界的哪一部分代表“宽度”和“高度”。请注意,frame 属性将为您提供 UIWindow 坐标空间中视图的矩形,该坐标空间(默认情况下)不会考虑界面方向。


实际上,当 ipad 面向横向时,这不起作用。即,如果我的模拟器正在运行横向,那么它仍然返回 768(而不是 1024)。你认为我应该有一个 if 语句来检查这种情况下的方向还是有更好的方法来获得宽度?
该死——看起来你是对的;我已经用更多想法更新了我的答案。
我最终只使用了“CGFloat width = CGRectGetWidth(self.view.bounds);”在我的方法中(不需要 didRotateFromInterfaceOrientation)......所以至少 self.view.bounds 考虑了方向。谢谢!!
对于其他可能偶然发现这篇旧帖子的人,我发现 self.view.bounds 仅从 didRotateFromInterfaceOrientation 开始完全准确。如果视图以其初始目标方向显示(在 IB 中指定),它在 viewDidLoad 和 viewWillAppear 中也是准确的。如果视图以不同的方向显示然后定位,则在 didRotateFromInterfaceOrientation 之前调用 viewDidLoad(和 viewWillAppear),并且 view.bounds 将不正确
在 iOS 6 的情况下,当我们使用 Modal 控制器时,不会调用 didRotateFromInterfaceOrientation
佚名
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
//Bonus height.
CGFloat height = CGRectGetHeight(screen);

不考虑方向
m
memmons

这可以在 3 行代码中完成:

// grab the window frame and adjust it for orientation
UIView *rootView = [[[UIApplication sharedApplication] keyWindow] 
                                   rootViewController].view;
CGRect originalFrame = [[UIScreen mainScreen] bounds];
CGRect adjustedFrame = [rootView convertRect:originalFrame fromView:nil];

这很接近,但如果 UIAlertView 正在显示,它将是关键窗口,它的 rootViewController 属性将为 nil
@mattbezark 是的。我认为这是一个极端情况。有一种替代方法可以获取根视图——不像这个那样干净,但我会发布替代方法以防万一有人感兴趣。
@memmons 参加聚会有点晚了,但我会对你的替代解决方案感兴趣:)
N
Nick

利用:

NSLog(@"%f",[[UIScreen mainScreen] bounds].size.width) ;

B
Bobby

从 iOS 9.0 开始,无法可靠地获取方向。这是我为仅纵向模式设计的应用程序使用的代码,因此如果应用程序以横向模式打开,它仍然是准确的:

screenHeight = [[UIScreen mainScreen] bounds].size.height;
screenWidth = [[UIScreen mainScreen] bounds].size.width;
if (screenWidth > screenHeight) {
    float tempHeight = screenWidth;
    screenWidth = screenHeight;
    screenHeight = tempHeight;
}

S
Shahzaib Maqbool

使用此代码将有所帮助

[[UIScreen mainScreen] bounds].size.height
[[UIScreen mainScreen] bounds].size.width

E
Esqarrouth

这是一种获取屏幕尺寸的 Swift 方法,这也考虑了当前界面方向:

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