iPhone/iPad:如何以编程方式获取屏幕宽度? [英] IPhone/IPad: How to get screen width programmatically?

查看:29
本文介绍了iPhone/iPad:如何以编程方式获取屏幕宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi I'm wondering if there's a way to get the width programmatically.

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

I'm looking for something general enough to accomodate iphone 3gs, iphone 4, ipad. Also, the width should change based on if the device is portrait or landscape (for ipad).

有人知道怎么做吗??找了好久了...谢谢!

Anybody know how to do this?? I've been looking for a while... thanks!

推荐答案

看一看 用户界面.

例如

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

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

Take a look at the applicationFrame property if you don't want the status bar included (won't affect the width).

更新: 结果表明 UIScreen(-bounds 或 -applicationFrame)没有考虑当前的界面方向.更正确的方法是询问您的 UIView 的边界 - 假设此 UIView 已由其视图控制器自动旋转.

UPDATE: It turns out UIScreen (-bounds or -applicationFrame) doesn't take into account the current interface orientation. A more correct approach would be to ask your UIView for its bounds -- assuming this UIView has been auto-rotated by it's View controller.

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

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

If the view is not being auto-rotated by the View Controller then you will need to check the interface orientation to determine which part of the view bounds represents the 'width' and the 'height'. Note that the frame property will give you the rect of the view in the UIWindow's coordinate space which (by default) won't be taking the interface orientation into account.

这篇关于iPhone/iPad:如何以编程方式获取屏幕宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆