shouldAutorotate,supportedInterfaceOrientations和preferredInterfaceOrientationForPresentation在iOS 7中无法正常工作

时间:2013-12-03 15:27:51

标签: uinavigationcontroller ios7 uitabbarcontroller

我在尝试阻止某些视图中的方向时遇到问题,但代码不起作用。

我在每个视图中使用这一行:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationMaskPortrait;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

它几乎可以在使用UINavigationController的视图中工作,但是在使用UITabBarController的视图中我遇到了很大的问题,因为它接触到的代码没有被调用。

1 个答案:

答案 0 :(得分:14)

好的我解决了,你必须继承UINavigationController和UITabBarController,所以这里是代码:

//cCustomNavigationController.h file

#import <UIKit/UIKit.h>

@interface cCustomNavigationController : UINavigationController <UINavigationControllerDelegate>

@end

//cCustomNavigationController.m file

#import "cCustomNavigationController.h"

@interface cCustomNavigationController ()

@end

@implementation cCustomNavigationController 

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

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

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

@end

//cCustomTabController.h file

#import <UIKit/UIKit.h>

@interface cCustomTabController : UITabBarController <UITabBarControllerDelegate>

@end

//cCustomTabController.m file

#import "cCustomTabController.h"

@interface cCustomTabController  ()

@end

@implementation cCustomTabController

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

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

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

@end

现在您只需要在需要的地方使用这些类创建TabBarController或NavigationController,即

//For the UINavigationController
UINavigationController *navigationController = [[cCustomNavigationController alloc] init];

//For the UITabBarController
UITabBarController *tabController = [[cCustomTabController alloc] init];

我希望这可以帮到你们。