2

I have a UINavigationController where all UIViewController in the stack are portrait, except the last one, which is landscape. My current implementation shows the last view controller in portrait on push, but I can rotate to landscape, and the, cannot rotate back to portrait. How can I force the rotation to landscape when the view is pushed ?

My UINavigationController subclass :

@interface RotationViewController : UINavigationController

@end

@implementation RotationViewController

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    if (self.topViewController.class == [LoadingViewController class])
    {
        return UIInterfaceOrientationMaskLandscape;
    }

    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    if (self.topViewController.class == [LoadingViewController class])
    {
        return UIInterfaceOrientationLandscapeLeft;
    }

    return UIInterfaceOrientationPortrait;
}

@end

The view controller that I want landscape-only :

@implementation LoadingViewController

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft;
}

@end

Other view controllers don't implement any of these methods.

4

2 回答 2

1

我有同样的条件。所以您可以在您的应用程序中实现以下代码

- (void)viewDidAppear:(BOOL)animated
{ 
    [super viewDidAppear:animated];

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(270));
    landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
    [[self navigationController].view setTransform:landscapeTransform];
    self.navigationController.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
    self.navigationController.navigationBar.frame = CGRectMake(0.0, 20.0, 480, 44.0); 
} 

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];   

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(0));
    landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
    [[self navigationController].view setTransform:landscapeTransform];
    self.navigationController.view.bounds = CGRectMake(0.0, 0.0, 320, 480);
    self.navigationController.navigationBar.frame = CGRectMake(0.0, 20.0, 320.0, 44.0);
}

#define degreesToRadian(X) (M_PI * (X)/180.0)

笔记:-

上面的代码是为 iphone 实现的。因此,如果您将其用于 ipad,请更改边界。

您无需实施

- (BOOL)shouldAutorotate

- (NSUInteger)supportedInterfaceOrientations

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

视图控制器中的函数....所以删除这些方法....

于 2013-02-28T05:30:37.597 回答
0

检查问题如何在 iOS 6 中处理不同的方向。有关您需要的项目示例,请参阅那里的答案。

基本上,您需要在您的视图控制器(您要旋转的那个)中嵌入一个自定义导航控制器。在这个自定义导航控制器中添加以下方法

- (NSUInteger)supportedInterfaceOrientations
{
    return self.topViewController.supportedInterfaceOrientations;
}

并添加到应该旋转的视图控制器:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

确保在您的项目中启用纵向、横向右侧和横向左侧方向。然后,如果您想阻止特定视图的某些方向:

– application:supportedInterfaceOrientationsForWindow:
于 2014-03-13T10:43:52.157 回答