某一页面强制横屏:
一、首先
在项目里面设置Device Orientation允许转屏的方向,下面设置了竖屏,和横屏(横屏左右根据home键确定):
二、AppDelagate代码
AppDelagate.h文件
@property (nonatomic,assign)BOOL allowRotation;//方向
AppDelagate.m文件
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)nowWindow {
if (_allowRotation == YES) {
return UIInterfaceOrientationMaskLandscapeLeft;
}
return UIInterfaceOrientationMaskPortrait;
}
三、然后在需要横屏的页面里:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self allowRotationIsYesOrNo:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self allowRotationIsYesOrNo:NO];
}
-(void)allowRotationIsYesOrNo:(BOOL)isYesOrNo{
AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.allowRotation = isYesOrNo;
[self SetTheScreenDirection:isYesOrNo];//调用转屏代码
}
//转屏
- (void)SetTheScreenDirection:(BOOL)fullscreen
{
if (fullscreen) {
NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}else{
NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
}