首先需要处理根视图 假如是navgationcontroller 写一个类继承与uinavgationcontroller 在这个子类中下以下代码;
#pragma mark - 只能竖屏设置 同时要在nagation和tabbarviewcontroller里面做相应的设置
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return self.topViewController.preferredStatusBarStyle;
}
如果根视图为UITabBarController 在tabbar的子类写如下代码
#pragma mark - 只能竖屏设置 同时要在nagation和tabbarviewcontroller里面做相应的设置
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
- (BOOL)shouldAutorotate
{
return self.selectedViewController.shouldAutorotate;
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return self.selectedViewController.preferredStatusBarStyle;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return self.selectedViewController.supportedInterfaceOrientations;
}
跟视图为MMDrawerController 在MMDrawerController 添加下面代码
//6.0之后系统调用该方法
-(BOOL)shouldAutorotate
{
//返回顶层视图的设置
if (self.openSide ==0) {
//centerShow
UITabBarController *mainTab = ( UITabBarController*)self.centerViewController;
return mainTab.selectedViewController.supportedInterfaceOrientations;
}
return NO;
}
//6.0之后系统调用该方法
-(NSUInteger)supportedInterfaceOrientations
{
//返回顶层视图支持的旋转方向
if (self.openSide ==0) {
//centerShow
UITabBarController *mainTab = ( UITabBarController*)self.centerViewController;
return mainTab.selectedViewController.supportedInterfaceOrientations;
}else if(self.openSide ==1){
//leftShow
}
return 0;
}
在每个viewcontroller中写以下的代码支持横竖屏:
#pragma mark - 只能竖屏设置 同时要在nagation和tabbarviewcontroller里面做相应的设置
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait ;//只支持竖屏
return UIInterfaceOrientationMaskLandscape;//只支持横屏
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
return YES;
}