如何获得与方向相关的屏幕高度和宽度? [英] How to get orientation-dependent height and width of the screen?

查看:100
本文介绍了如何获得与方向相关的屏幕高度和宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式确定应用程序的当前高度和宽度。我用这个:

I'm trying to programmatically determine the current height and width of my application. I use this:

CGRect screenRect = [[UIScreen mainScreen] bounds];

但无论设备是否处于纵向状态,这都会产生320的宽度和480的高度或横向。如何确定主屏幕的当前宽度和高度(即取决于设备方向)?

But this yields a width of 320 and a height of 480, regardless of whether the device is in portrait or landscape orientation. How can I determine the current width and height (i.e. dependent upon the device orientation) of my main screen?

推荐答案

您可以使用类似 UIInterfaceOrientationIsPortrait([UIApplication sharedApplication] .statusBarOrientation)的内容来确定方向,然后相应地使用尺寸。

You can use something like UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) to determine the orientation and then use the dimensions accordingly.

但是,在UIViewController的方向更改期间

HOWEVER, during an orientation change like in UIViewController's

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                 duration:(NSTimeInterval)duration

使用中传递的方向toInterfaceOrientation 因为UIApplication的statusBarOrientation仍将指向旧方向,因为它还没有改变(因为你在事件处理程序内)。

Use the orientation passed in toInterfaceOrientation since the UIApplication's statusBarOrientation will still point to the old orientation as it has not yet changed (since you're inside a will event handler).

摘要

有几种相关的帖子,但每个都似乎表明你必须:

There are several related posts to this, but each of them seem to indicate that you have to:


  1. 看看 [[UIScreen mainScreen] bounds] 获取尺寸,

  2. 检查您的方向,

  3. 帐户状态条形高度(如果显示)

  1. Look at [[UIScreen mainScreen] bounds] to get the dimensions,
  2. Check what orientation you are in, and
  3. Account for the status bar height (if shown)

链接

  • Iphone: Get current view dimensions or screen dimensions
  • IPhone/IPad: How to get screen width programmatically?
  • Objective C - how to get current screen resolution?
  • "Incorrect" frame / window size after re-orientation in iPhone or iPad
  • iPhone Dev SDK - get screen width

工作代码

我通常不会走这么远,但你引起了我的兴趣。以下代码应该可以解决问题。我在UIApplication上写了一个类别。我添加了类方法来获取currentSize或给定方向的大小,这是你在UIViewController的 willRotateToInterfaceOrientation中调用的:duration:

I usually don't go this far, but you piqued my interest. The following code should do the trick. I wrote a Category on UIApplication. I added class methods for getting the currentSize or the size in a given orientation, which is what you would call in UIViewController's willRotateToInterfaceOrientation:duration:.

@interface UIApplication (AppDimensions)
+(CGSize) currentSize;
+(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation;
@end

@implementation UIApplication (AppDimensions)

+(CGSize) currentSize
{
    return [UIApplication sizeInOrientation:[UIApplication sharedApplication].statusBarOrientation];
}

+(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation
{
    CGSize size = [UIScreen mainScreen].bounds.size;
    UIApplication *application = [UIApplication sharedApplication];
    if (UIInterfaceOrientationIsLandscape(orientation))
    {
        size = CGSizeMake(size.height, size.width);
    }
    if (application.statusBarHidden == NO)
    {
        size.height -= MIN(application.statusBarFrame.size.width, application.statusBarFrame.size.height);
    }
    return size;
}

@end

使用代码简单调用 [UIApplication currentSize] 。此外,我运行上面的代码,所以我知道它的工作原理并报告所有方向的正确响应。请注意,我考虑状态栏。有趣的是,我不得不减去状态栏高度和宽度的MIN。

To use the code simple call [UIApplication currentSize]. Also, I ran the above code, so I know it works and reports back the correct responses in all orientations. Note that I factor in the status bar. Interestingly I had to subtract the MIN of the status bar's height and width.

希望这会有所帮助。 :D

Hope this helps. :D

其他想法

您可以通过查看来获取尺寸在UIWindow的 rootViewController 属性中。我在过去看过这个,它同样在纵向和横向上报告相同的尺寸,除了它报告有一个旋转变换:

You could go about getting the dimensions by looking at the UIWindow's rootViewController property. I've looked at this in the past and it similarly reports the same dimensions in both portrait and landscape except it reports having a rotate transform:


(gdb) po [[[[UIApplication sharedApplication] keyWindow]
rootViewController] view]

(gdb) po [[[[UIApplication sharedApplication] keyWindow] rootViewController] view]

< UILayoutContainerView:0xf7296f0; frame =
(0 0; 320 480); transform = [0,-1,1,0,0,0]; autoresize = W + H;
layer =< CALayer:0xf729b80>>

<UILayoutContainerView: 0xf7296f0; frame = (0 0; 320 480); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0xf729b80>>

(gdb) po [[[[UIApplication
sharedApplication ] keyWindow] rootViewController] view]

(gdb) po [[[[UIApplication sharedApplication] keyWindow] rootViewController] view]

< UILayoutContainerView:0xf7296f0; frame =(0 0; 320 480); autoresize
= W + H; layer =< CALayer:0xf729b80>>

<UILayoutContainerView: 0xf7296f0; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0xf729b80>>

不确定您的应用是如何工作的,但如果您没有使用导航控制器在某种情况下,您可以在主视图下使用父级的最大高度/宽度进行UIView,并使用父级增长/缩小。然后你可以这样做: [[[[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] subviews] objectAtIndex:0] frame] 。在一行看起来相当激烈,但你明白了。

Not sure how your app works, but if you aren't using a navigation controller of some kind, you could have a UIView under your main view with the max height / width of parent and grows / shrinks with parent. Then you could do: [[[[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] subviews] objectAtIndex:0] frame]. That looks pretty intense on one line, but you get the idea.

然而......在摘要下进行上述3个步骤仍然会更好。开始搞乱UIWindows,你会发现奇怪的东西,比如显示UIAlertView将改变UIApplication的keywindow指向UIAlertView创建的新UIWindow。谁知道?在找到依赖keyWindow的bug并发现它改变了之后我做了!

However... It would still be better to do the above 3 steps under the summary. Start messing with UIWindows and you'll find out weird stuff, like showing a UIAlertView will change UIApplication's keywindow to point at a new UIWindow that the UIAlertView created. Who knew? I did after finding a bug relying on keyWindow and discovering that it changed like that!

这篇关于如何获得与方向相关的屏幕高度和宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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