如何将屏幕旋转为横向?

4
如何将屏幕旋转到横向显示?
您能提供简单的代码建议吗?

这是 https://dev59.com/13VD5IYBdhLWcg3wXaid 或 http://stackoverflow.com/questions/1437636/how-to-autorotate-from-portrait-to-landscape-mode 的副本。 - Brad Larson
5个回答

2
在UIViewController中,您应该拥有这个方法:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
 return YES;
}

为了实现自动旋转,请按照以下步骤操作:

如果您的应用程序具有 uiTabBarController,则必须对其进行子类化,并向其中添加该方法。像这样:

@interface MyTabBarController : UITabBarController {

}

@end

#import "MyTabBarController.h"

@implementation MyTabBarController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

@end

shouldAutorotateToInterfaceOrientation: 在iOS 6.0中已被弃用。请参见此链接:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instm/UIViewController/shouldAutorotateToInterfaceOrientation:。 - JohnK

2

这比你最初想象的要棘手!经过讨论,这篇博客文章(附有进一步讨论的链接)包含了最干净的答案:

如何随意切换到横屏模式


0

只需在您的视图控制器文件中实现此功能,就完成了。

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
{
     return YES;
}

shouldAutorotateToInterfaceOrientation: 在iOS 6.0中已被弃用。请参见此链接:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instm/UIViewController/shouldAutorotateToInterfaceOrientation:。 - JohnK

0
感谢SorinA。需要UITabBarControllershouldAutorotateToInterfaceOrientation:让我感到非常沮丧,因为它不是我出于任何原因而进行子类化的类(即,对于我的应用程序来说,默认功能很好)。
作为比子类化更轻量级的解决方案,我使用了一个类别(也许它们都是一样的,但它看起来更轻量级)。
@interface UITabBarController (WithRotation)
@end

@implementation UITabBarController (WithRotation)
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation {
    return YES;
}
@end

p.s. 如果您只想支持某些屏幕方向,请测试接口方向并仅在适当的方向返回YES。

p.p.s. 什么是info.plist中的Supported interface orientations?唯一重要的事情似乎是从shouldAutorotateToInterfaceOrientation:返回的内容。


shouldAutorotateToInterfaceOrientation: 在iOS 6.0中已被弃用。请参见此处 - JohnK


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接