iphone - iPhone (/iPad) 上的高度和宽度

标签 iphone objective-c ipad interface uiwindow

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

- (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;
}

如果没有设置状态栏方向的第一行,即使界面的其余部分是横向左侧,警报 View 也会纵向显示。

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

win 768.000000 - 1024.000000

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

Apple 似乎真的搞砸了界面旋转,因为我除了问题什么都没有,我不记得 iPhone 上发生过任何这种情况,所以请有人告诉我如何解决这个问题。

我会给至少能解释为什么会发生这种情况并希望提供解决方案的人 500 点声望(这只是我声望的 10 点)。

最佳答案

我认为 "Launching in Landscape Mode"iOS Application Programming Guide主要解释您的测试应用程序发生了什么:

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:

  • 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.

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

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.

就您的测试应用而言,关键部分是最后一节。您没有 View Controller ,因此您完全负责按照您想要的方向设置 UI。这就是您必须手动设置状态栏方向的原因。

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

目前尚不清楚您要通过您的真实应用完成什么,但推荐的做法是将您的 View 布置为纵向,然后设置它们的自动调整大小属性以处理自动旋转(无论它是否在应用启动后立即发生或以后)。

关于iphone - iPhone (/iPad) 上的高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3972001/

相关文章:

iPhone耳机点击检测

iphone - 在iPhone上轻按闪光灯

iPhone 用户界面选择器

iphone - xcode:使用带有 Storyboard的 iPad 模拟器运行时出现黑屏

iphone - 调整 UIView 的大小而不使用动画

ios - 是否可以仅针对 4 英寸 iPhone 显示屏进行构建?

ios - 如何为多个 UIView 添加撤消管理器

ios - Core Data SIGABRT 什么时候取数据?

iOS App Review Fail - 装有 iOS 7.1 的 iPad 在启动时崩溃

iphone - 谁能给我解释一下 iPhone 中字典的实际结构?