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

编程入门 行业动态 更新时间:2024-10-28 16:26:53
本文介绍了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:如何以编程方式获取屏幕宽度?

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

发布评论

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

>www.elefans.com

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