【问题标题】:IPhone/IPad: How to get screen width programmatically?iPhone/iPad:如何以编程方式获取屏幕宽度?
【发布时间】:2010-09-07 00:54:07
【问题描述】:

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

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

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

【问题讨论】:

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

标签: iphone objective-c cocoa-touch ipad


【解决方案1】:

看看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
【解决方案2】:
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
//Bonus height.
CGFloat height = CGRectGetHeight(screen);

【讨论】:

  • 不考虑方向
【解决方案3】:

这可以在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 参加聚会有点晚了,但我会对你的替代解决方案感兴趣 :)
【解决方案4】:

使用:

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

【讨论】:

    【解决方案5】:

    从 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;
    }
    

    【讨论】:

      【解决方案6】:

      使用此代码会有所帮助

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

      【讨论】:

        【解决方案7】:

        这是一种获取屏幕尺寸的 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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-01-06
          • 2012-06-02
          • 1970-01-01
          • 2011-04-06
          • 1970-01-01
          • 2011-03-11
          • 1970-01-01
          相关资源
          最近更新 更多