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

编程入门 行业动态 更新时间:2024-10-28 18:30: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:如何以编程方式获取屏幕宽度?

本文发布于:2023-11-27 20:24:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639478.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:宽度   屏幕   方式   iPhone   iPad

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!