iPhone(/ iPad)上的高度和宽度 [英] Height and width on iPhone (/iPad)

查看:107
本文介绍了iPhone(/ iPad)上的高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是一个测试应用程序,只有一个AppDelegate类来创建我所做的就是创建一个基于Window的应用程序,将支持的方向设置为仅限于info.plist中的格局,然后添加以下代码: / p>

This is just a test application, There is only a AppDelegate class to create all I did was create a Window based app, set the supported orientations to only the landscape in the info.plist, and then add the following code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
[application setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];

// Override point for customization after application launch.
UIAlertView *test = [[UIAlertView alloc] initWithTitle:@"hu" message:@"hui" delegate:nil cancelButtonTitle:@"hi" otherButtonTitles:nil];
[test show];
[window makeKeyAndVisible];
    NSLog(@"win %f - %f", window.bounds.size.width, window.bounds.size.height);
return YES;
}

如果没有第一行设置状态栏方向,则会显示警报视图即使接口的其余部分处于横向左侧也是如此。

Without the first line, which sets the status bar orientation, the alert view appears in portrait even though the rest of the interface is in landscape left.

无论如何,Log仍然给出了这个:

Anyway the Log still gives this:

win 768.000000 - 1024.000000

这是错误的回合方式(因此,当我在我的真实应用程序中添加子视图时,框架不正确)

This is the wrong way round (and thus when I add subviews in my real app the frames are not correct)

Apple似乎已经在界面轮换中捣乱,因为我什么也没有问题,我不记得在iPhone上发生过这种情况,所以请有人告诉我如何解决这个问题。

Apple seems to have really mucked up on the interface rotation, because I've had nothing but problems, I don't remember any of this happening on the iPhone, so please can someone tell me how to fix this.

我会给500点声望(这就是全部但至少可以解释为什么会这样,并希望提供解决方案。

I'll give 500 reputation (that's all but 10 of my reputation) to the person who can at least explain why this happens and hopefully provide a solution.

推荐答案

我认为以横向模式启动 iOS应用编程指南主要解释您的测试应用程序发生了什么:

I think the "Launching in Landscape Mode" of the iOS Application Programming Guide mostly explains what is happening with your test application:


iOS中的应用程序通常以
纵向模式启动以匹配方向主屏幕
。如果你有一个以b
和横向模式运行的
应用程序,你的应用程序
应该始终以纵向模式
启动,然后让它查看
控制器旋转根据设备的
方向,需要
的界面。但是,如果您的应用程序仅在横向模式下运行
,则
必须执行以下步骤才能
使其最初以横向
方向启动:

Applications in iOS normally launch in portrait mode to match the orientation of the Home screen. If you have an application that runs in both portrait and landscape mode, your application should always launch in portrait mode initially and then let its view controllers rotate the interface as needed based on the device’s orientation. If your application runs in landscape mode only, however, you must perform the following steps to make it launch in a landscape orientation initially:


  • 在应用程序的Info.plist文件中,
    添加UIInterfaceOrientation键,
    将其值设置为横向模式。
    对于横向方向,您可以
    将此键的值设置为
    UIInterfaceOrientationLandscapeLeft或
    UIInterfaceOrientationLandscapeRight。

  • In your application’s Info.plist file, add the UIInterfaceOrientation key and set its value to the landscape mode. For landscape orientations, you can set the value of this key to UIInterfaceOrientationLandscapeLeft or UIInterfaceOrientationLandscapeRight.

以横向模式
布局您的视图,并确保正确设置其自动调整
选项。

Lay out your views in landscape mode and make sure that their autoresizing options are set correctly.

覆盖视图控制器的
shouldAutorotateToInterfaceOrientation:
方法,仅对
所需的横向方向返回YES,为纵向方向返回NO

Override your view controller’s shouldAutorotateToInterfaceOrientation: method and return YES only for the desired landscape orientation and NO for portrait orientations.

重要事项:前面的步骤假定
您的应用程序使用视图控制器
来管理其视图层次结构。查看
控制器提供了大量
的基础设施,用于处理
方向更改以及其他
复杂视图相关事件。如果您的
应用程序未使用视图
控制器 - 可能是
游戏和其他基于OpenGL ES的
应用程序的情况 - 您需要负责
旋转绘图表面(或
调整绘图命令)为
,需要以
横向模式显示您的内容。

Important: The preceding steps assume your application uses view controllers to manage its view hierarchy. View controllers provide a significant amount of infrastructure for handling orientation changes as well as other complex view-related events. If your application is not using view controllers—as may be the case with games and other OpenGL ES–based applications—you are responsible for rotating the drawing surface (or adjusting your drawing commands) as needed to present your content in landscape mode.

就测试应用而言,关键部分是最后一部分。您没有视图控制器,因此您完全有责任根据需要设置UI。这就是您必须手动设置状态栏方向的原因。

In terms of your test application, the key part is the last section. You do not have a view controller, so you are entirely responsible for setting up the UI in the orientation you want. That is why you have to set the status bar orientation manually.

我读了第一段,说iOS应用程序总是以纵向模式启动,然后根视图控制器旋转其视图以立即匹配设备方向,一旦它没有动画被添加到窗口中。这意味着UIWindow本身不会旋转,因此其尺寸将始终以纵向方向(如tadej5553所述)。此外,所有UIWindow子视图的帧也将是纵向方向(因为帧始终在父视图的坐标中定义)。因此,无论您如何旋转设备,根视图控制器的框架始终都是纵向方向。但是,由于视图的边界属性是根据自己的坐标定义的,因此高度和宽度应该反映视图的当前方向。

I read the first paragraph as saying that iOS apps always launch in portrait mode and then the root view controller rotates its view to match the device orientation immediately and without animation once it is added to the window. That means the UIWindow itself does not rotate so its dimensions will always be in terms of a portrait orientation (as tadej5553 has said). In addition, the frames of all of the UIWindow subviews will also be in terms of a portrait orientation (since the frame is always defined in the parent view's coordinates). So, no matter how you rotate the device, the root view controller's frame will always be in terms of a portrait orientation. However, since a view's bounds property is defined in terms of its own coordinates, that height and width should reflect the current orientation of the view.

目前尚不清楚你是什么正在尝试使用您的真实应用程序完成,但建议的做法是为纵向布局您的视图,然后设置其自动调整属性以处理自动旋转(无论是在应用程序启动后或之后立即发生)。

It is not clear what you are trying to accomplish with your real app, but the recommended practice is to lay out your views for portrait orientation and then set their autoresizing properties to handle the automatic rotation (whether it occur immediately after app launch or later).

这篇关于iPhone(/ iPad)上的高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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