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

查看:279
本文介绍了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!

推荐答案

查看 UIScreen

例如

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).

UPDATE:结果是UIScreen(-bounds或-applicationFrame)没有考虑当前界面方向。更正确的方法是请求你的UIView的边界 - 假设这个UIView已被它的View控制器自动旋转。

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的坐标空间中的视图的rect(默认情况下)不会考虑界面方向。

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天全站免登陆