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

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

问题描述

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

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.

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

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 点声望(这只是我的 10 点声望).

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.

推荐答案

我认为 "以横向模式启动"/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007072-CH1-SW1" rel="noreferrer">iOS 应用程序编程指南 主要解释您的测试应用程序发生了什么:

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

iOS 中的应用程序通常在纵向模式以匹配方向的主屏幕.如果你有一个在两种纵向模式下运行的应用程序和横向模式,您的应用程序应始终以纵向模式启动最初,然后让它的观点控制器将界面旋转为需要根据设备的方向.如果您的应用程序运行但是,仅在横向模式下,您必须执行以下步骤才能让它在风景中发射最初的方向:

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:方法并只为所需的横向方向和 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 子视图的框架也将是纵向的(因为框架总是在父视图的坐标中定义).因此,无论您如何旋转设备,根视图控制器的框架始终是纵向的.然而,由于视图的 bounds 属性是根据它自己的坐标定义的,所以高度和宽度应该反映视图的当前方向.

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天全站免登陆