1.核心代码
具体的VC里:
//这里是为了旋转的时候调用
-(void)setNavigation:(BOOL)isLand{
DY_mainTabBarViewController * tab = (DY_mainTabBarViewController *)self.tabBarController;
//切换rootViewController的旋转方向
if (isLand) {
tab.interfaceOrientation = UIInterfaceOrientationLandscapeRight;
tab.interfaceOrientationMask = UIInterfaceOrientationMaskLandscapeRight;
//设置屏幕的转向为横屏
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeRight) forKey:@"orientation"];
}
else {
tab.interfaceOrientation = UIInterfaceOrientationPortrait;
tab.interfaceOrientationMask = UIInterfaceOrientationMaskPortrait;
//设置屏幕的转向为竖屏
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
}
//刷新
[UIViewController attemptRotationToDeviceOrientation];
}
根视图里:(主要的难点就是区分方法只能在根视图里调用,由于我的项目根视图是在tabBarcontroller里 ,所以在tabBarcontroller重新一下方法)
//设置是否允许自动旋转
- (BOOL)shouldAutorotate {
return YES;
}
//设置支持的屏幕旋转方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return self.interfaceOrientationMask ;
}
注意:一定要选择了方向。代码才会有作用。
网友评论