无法停止设备旋转

时间:2014-04-20 07:14:45

标签: ios uinavigationcontroller uitabbarcontroller ios6.1

我发布了一个问题here。其中一位好友回复并给出了解决方案。虽然解决方案是在少数视图控制器上工作,但在视图上它不起作用。当我进入一个具有TabController +导航控制器的视图控制器时,该选项卡栏项目,代码不起作用。并且视图可以旋转。

我使用iOS 6.1的以下代码

//For iOS6
- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
- (NSUInteger)supportedInterfaceOrientations {
   return UIInterfaceOrientationMaskPortrait;
}

我也必须在iOS 7中实现相同的功能。 请帮忙

3 个答案:

答案 0 :(得分:2)

shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation

如果viewcontroller位于tabbarcontroller的任何navigationcontroller内,则不会调用上述方法。如果在tabbarcontroller或导航控制器中声明了这些方法,那么它们将被调用。

要解决此问题,请创建一个类FixedOrientationTab,它是UITabBarController的子类,以及一个导航类OrientationEnabledNavigation,它是UINavigationController的子类。然后在shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentationFixedOrientationTab内实施了OrientationEnabledNavigation个方法。

<强> OrientationEnabledNavigation.h

#import <UIKit/UIKit.h>

@interface OrientationEnabledNavigation : UINavigationController

@end

<强> OrientationEnabledNavigation.m

#import "OrientationEnabledNavigation.h"

@interface OrientationEnabledNavigation ()

@end

@implementation OrientationEnabledNavigation

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
//    return NO;
}

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

<强> FixedOrientationTab.h

#import <UIKit/UIKit.h>

@interface FixedOrientationTab : UITabBarController

@end

<强> FixedOrientationTab.m

#import "FixedOrientationTab.h"

@interface FixedOrientationTab ()

@end

@implementation FixedOrientationTab

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
    //    return NO;
}

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

然后,如果您想在项目中使用导航控制器,请使用OrientationEnabledNavigation和tabbar FixedOrientationTab。之后,如果您在shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation中实施viewcontroller这些方法,则会调用它们。

希望这会有所帮助.. :)

修改 正如Jeev指出的另一种方法是添加一些代码开头的代码:

UITabBarController

@implementation UITabBarController (AutoRotationForwarding)

-(BOOL)shouldAutorotate
{
    if ([self.selectedViewController respondsToSelector:@selector(shouldAutorotate)]) {
        return [self.selectedViewController shouldAutorotate];
    }
    else {
        return YES;
    }
}

- (NSUInteger)supportedInterfaceOrientations {
    if ([self.selectedViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
        return [self.selectedViewController supportedInterfaceOrientations];
    }
    else {
        return UIInterfaceOrientationMaskAll;
    }
}

@end

UINavigationController

@implementation UINavigationController (AutoRotationForwarding)

-(BOOL)shouldAutorotate
{
    if ([self.topViewController respondsToSelector:@selector(shouldAutorotate)]) {
        return [self.topViewController shouldAutorotate];
    }
    else {
        return YES;
    }
}

-(NSUInteger) supportedInterfaceOrientations {
    if([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
    {
        return [self.topViewController supportedInterfaceOrientations];
    }
    return UIInterfaceOrientationMaskPortrait;
}

@end

答案 1 :(得分:0)

我有UINavigationController包含UIViewController。

您应该通过子类UINavigationController在UINavigationController中禁用rotate并实现

- (BOOL)shouldAutorotate
{
return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
// return some
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
// return some thing that you want
}

希望这会对你有所帮助。

答案 2 :(得分:0)

由于我使用TabBarController,我得到的唯一可行解决方案是创建一个类别。

所以只需制作一个类别

这是类别的.h类,我将其命名为AutoRotationtab

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

@interface UITabBarController (AutoRotationTab)

@end

这是类别的.m类

#import "UITabBarController+AutoRotationTab.h"

@implementation UITabBarController (AutoRotationTab)

-(BOOL)shouldAutorotate
{
if(globalvariable_shouldRotate)
{
    return YES;
}
else
{
    return NO;
}
}


- (NSUInteger)supportedInterfaceOrientations
{

if(globalvariable_shouldRotate)
{
    return UIInterfaceOrientationMaskAll;
}
else
{
    return UIInterfaceOrientationMaskPortrait;
}
}

只需制作此类别,并查看魔术,它的作品