在AppDelegate.h中定義這兩個(gè)屬性
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nonatomic, assign) BOOL allowRotation;//允許旋轉(zhuǎn)
@property (nonatomic, assign) BOOL rightRotation;//右側(cè)旋轉(zhuǎn)
@end
在AppDelegate.m中添加下方代碼
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
? ? if (self.allowRotation) {
? ? ? ? return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight;
? ? }elseif(self.rightRotation) {
? ? ? ? return UIInterfaceOrientationMaskLandscapeRight;
? ? }
? ? return UIInterfaceOrientationMaskPortrait;
}
在想要橫屏?xí)r
AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
? ? ? ? appdelegate.rightRotation=YES;
? ? ? ? [appdelegateapplication:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
? ? ? ? //強(qiáng)制翻轉(zhuǎn)屏幕厕倍,Home鍵在右邊贩疙。
? ? ? ? [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeRight) forKey:@"orientation"];
? ? ? ? //刷新
? ? ? ? [UIViewController attemptRotationToDeviceOrientation];
豎屏?xí)r
AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
? ? ? ? appdelegate.rightRotation=NO;
? ? ? ? [appdelegateapplication:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
?? ? ? //強(qiáng)制翻轉(zhuǎn)屏幕
?? ? ? [[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
?? ? ? //刷新
?? ? ? [UIViewController attemptRotationToDeviceOrientation];