从横向视图控制器推动时强制纵向方向

时间:2013-01-31 19:04:31

标签: iphone objective-c orientation landscape portrait

应用支持:iOS6 +

我的应用程序适用于纵向和横向。但是1个控制器应该仅适用于肖像。

问题在于,当我在横向并推动视图控制器时,新的视图控制器也处于横向状态,直到我将其旋转为纵向。然后它应该像是应该是肖像。

是否可以始终以肖像方式显示?即使它的父母在风景中推动它?

以下所有代码均无效

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];

这段代码一直有效,除非我不从景观How to force a UIViewController to Portrait orientation in iOS 6推送

2 个答案:

答案 0 :(得分:31)

我通过在ViewDidLoad

中添加以下行来解决这个问题
UIViewController *c = [[UIViewController alloc]init];
[self presentViewController:c animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];

答案 1 :(得分:3)

首先,您需要创建一个类别:

的UINavigationController + Rotation_IOS6.h

#import <UIKit/UIKit.h>

@interface UINavigationController (Rotation_IOS6)

@end

的UINavigationController + Rotation_IOS6.m:

#import "UINavigationController+Rotation_IOS6.h"

@implementation UINavigationController (Rotation_IOS6)

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

@end

然后,在您的班级中实现这些方法,您只希望使用这些方法:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

如果您正在使用UITabBarController,只需替换UITabBarController的UINavigationController即可。 经过长时间的搜索,这个解决方案对我很有用!我和你现在的情况一样!

修改

所以,我看到了你的样本。你需要做一些改变。 1 - 为UINavigationController类别创建一个新类。将类命名为UINavigationController + Rotation_IOS6(.h和.m) 2 - 您不需要实现方法preferredInterfaceOrientationForPresentation。您的类别应如下所示:

#import "UINavigationController+Rotation_IOS6.h"

@implementation UINavigationController (Rotation_IOS6)

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

@end

3 - 在您想要仅在横向中旋转的类中,将其包含在实现中,完全如下:

// Rotation methods for iOS 6
- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

4 - 我建议在你想要的课程中包含iOS 5自动旋转的方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}