1、IOS8之后有的方法写到类里强制横屏之后已经没有用了
-(BOOL)shouldAutorotate{
return NO;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
2、IOS8之后该怎么实现强制横屏
首先在代理类实现该方法:
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
if (self.isForceLandscape) {
return UIInterfaceOrientationMaskLandscape;
}else if (self.isForcePortrait){
return UIInterfaceOrientationMaskPortrait;
}
return UIInterfaceOrientationMaskAll;
}
然后在代理类头文件里定义2个全局变量
/**
* 是否强制横屏
*/
@property BOOL isForceLandscape;
/**
* 是否强制竖屏
*/
@property BOOL isForcePortrait;
最后一步,在你所需要实现强制横屏的ViewController里添加如下方法
#pragma mark 横屏设置
/**
* 强制横屏
*/
-(void)forceOrientationLandscape{
//这种方法,只能旋转屏幕不能达到强制横屏的效果
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = UIInterfaceOrientationLandscapeLeft;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
//加上代理类里的方法,旋转屏幕可以达到强制横屏的效果
AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
appdelegate.isForceLandscape=YES;
[appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
}
/**
* 强制竖屏
*/
-(void)forceOrientationPortrait{
//加上代理类里的方法,旋转屏幕可以达到强制竖屏的效果
AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
appdelegate.isForcePortrait=YES;
[appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
}
/**
* 页面消失需要释放强制约束
*
* @param animated <#animated description#>
*/
-(void)viewWillDisappear:(BOOL)animated{
//释放约束
AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
appdelegate.isForcePortrait=NO;
appdelegate.isForceLandscape=NO;
[appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
//退出界面前恢复竖屏
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
-(void)viewDidAppear:(BOOL)animated{
//[self forceOrientationPortrait]; //设置竖屏
[self forceOrientationLandscape]; //设置横屏
}