在AppDelegate.h中定义这两个属性
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nonatomic, assign) BOOL allowRotation;//允许旋转
@property (nonatomic, assign) BOOL rightRotation;//右侧旋转
@end
在AppDelegate.m中添加下方代码
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.allowRotation) {
return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight;
}elseif(self.rightRotation) {
return UIInterfaceOrientationMaskLandscapeRight;
}
return UIInterfaceOrientationMaskPortrait;
}
在想要横屏时
AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
appdelegate.rightRotation=YES;
[appdelegateapplication:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
//强制翻转屏幕,Home键在右边。
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeRight) forKey:@"orientation"];
//刷新
[UIViewController attemptRotationToDeviceOrientation];
竖屏时
AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
appdelegate.rightRotation=NO;
[appdelegateapplication:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
//强制翻转屏幕
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
//刷新
[UIViewController attemptRotationToDeviceOrientation];