#1. 全局禁止横屏
Appdelegate.m添加如下代码:
-(UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window{
if(self.allowRotation){//如果设置了allowRotation属性,支持全屏
return UIInterfaceOrientationMaskAll;}
returnUIInterfaceOrientationMaskPortrait;//默认全局不支持横屏
}
#2. 在需要支持横屏的界面改变allowRotation属性
//进入全屏-(void)begainFullScreen{
AppDelegate*appDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];appDelegate.allowRotation=YES;
}
// 退出全屏-(void)endFullScreen{
AppDelegate*appDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
appDelegate.allowRotation=NO;
//强制归正:
if([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)])
{
SEL selector=NSSelectorFromString(@"setOrientation:");
NSInvocation*invocation=[NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
intval=UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
网友评论