我已经解决了这个问题并回答了,这样如果有人遇到同样的问题,他们就可以得到帮助。
shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
如果上述方法位于导航控制器的任何选项卡控制器内,则不会调用视图控制器。如果这些方法在tabarcontroller或导航控制器中声明,则将调用它们。在我的案例中,视图控制器位于导航控制器内,导航控制器位于选项卡控制器内。
为了解决这个问题,我创建了一个类FixedOrientationTab,它是UITabBarController的子类,以及一个导航类OrientationEnabledNavigation,它是UI导航控制器的子类。然后我实施了
shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
FixedOrientationTab和OrientationEnabledNavigation中的方法。
定向启用导航.h
#import <UIKit/UIKit.h>
@interface OrientationEnabledNavigation : UINavigationController
@end
方向启用导航.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
固定方向Tab.h
#import <UIKit/UIKit.h>
@interface FixedOrientationTab : UITabBarController
@end
固定方向Tab.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和选项卡FixedOrientationTab。之后,如果您实施
应自动旋转,支持界面定向,首选用于演示的界面定向
这些方法在viewcontroller中,那么它们将被调用。
希望这有帮助..:)