无法停止设备旋转
我在这里发布了一个问题。 其中一个好友回答并给出了解决办法。 虽然这个解决scheme在很less的视图控制器上工作,但是看起来不行。 当我进入一个视图控制器的TabController +导航控制器的那里标签栏项目,代码不起作用。 和意见是能够旋转。
我使用iOS 6.1的以下代码
//For iOS6 - (BOOL)shouldAutorotate { return NO; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; }
我也必须在iOS 7中实现同样的东西。 请帮忙
shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
如果上述方法位于navigationcontroller
任何tabbarcontroller
,则不会调用viewcontroller
。 如果这些方法是在tabbarcontroller
或导航控制器中声明的,那么他们将被调用。
为了解决这个问题,创build了一个FixedOrientationTab
类,它是UITabBarController
一个子类,和一个导航类OrientationEnabledNavigation
,它是UINavigationController
一个子类。 然后执行shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
方法在FixedOrientationTab
和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
我有UINavigationController包含UIViewController。
您应该通过子类UINavigationController禁用UINavigationController中的旋转并执行
- (BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { // return some } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { // return some thing that you want }
希望这可以帮助你。
由于我使用TabBarController,我得到的唯一工作解决scheme是创build一个类别。
所以只是做一个类别
这里是类的.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; } }
只是做这个类别,看看魔术,它的作品