【发布时间】:2011-01-17 19:38:16
【问题描述】:
如何将屏幕旋转为横向?
你能推荐简单的代码吗?
【问题讨论】:
标签: iphone
如何将屏幕旋转为横向?
你能推荐简单的代码吗?
【问题讨论】:
标签: iphone
本指南介绍了如何在应用程序中启用自动旋转: http://developer.apple.com/iphone/library/codinghowtos/UserExperience/index.html#GENERAL-HANDLE_AUTOROTATION_OF_MY_USER_INTERFACE
本指南介绍了如何以横向模式启动应用程序: http://developer.apple.com/iphone/library/codinghowtos/UserExperience/index.html#GENERAL-START_MY_APPLICATION_IN_LANDSCAPE_MODE
【讨论】:
这比你最初想象的要复杂!经过多次讨论,这篇博文(后面有进一步讨论的链接)包含最清晰的答案:
【讨论】:
在 uiViewController 你应该有方法
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
为了自动旋转。
如果您的应用程序有一个 uiTabBarController,那么您必须继承 UITabBarController 并将方法也添加到它。像这样的东西:
@interface MyTabBarController : UITabBarController {
}
@end
#import "MyTabBarController.h"
@implementation MyTabBarController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
@end
【讨论】:
感谢索琳娜。需要 UITabBarController shouldAutorotateToInterfaceOrientation: 给了我各种各样的挫败感,因为它不是我出于任何原因子类化的类(即,默认功能对我的应用程序来说很好)。
作为一种比子类化更轻量级的解决方案,我使用了一个类别(可能都一样,但看起来更轻量级)。
@interface UITabBarController (WithRotation)
@end
@implementation UITabBarController (WithRotation)
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation {
return YES;
}
@end
附言如果您只想支持某些方向,请测试 interfaceOrientation 并仅在适当的方向上返回 YES。
p.p.s. info.plist Supported interface orientations 是干什么用的?似乎唯一重要的是从shouldAutorotateToInterfaceOrientation:返回的内容
【讨论】:
只需在您的视图控制器文件中实现它, 你就完成了。
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
{
return YES;
}
【讨论】: