我项目中全部是竖屏,但是有那么几个是横屏,也有几个是横屏中要输入一些信息,然后就要使得键盘也是横屏。
1、设置项目的Device Landscape中的信息,要勾选这么几项,如下:
2、设置强制横屏代码
在viewDidLoad方法里面添加如下代码:
- (void)viewDidLoad {
[superviewDidLoad];
if ([[UIDevicecurrentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocationinvocationWithMethodSignature:[UIDeviceinstanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevicecurrentDevice]];
int val =UIInterfaceOrientationLandscapeRight;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
-(void)viewWillDisappear:(BOOL)animated
{
if ([[UIDevicecurrentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocationinvocationWithMethodSignature:[UIDeviceinstanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevicecurrentDevice]];
int val =UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
其实viewWillDisappear代码也就是强制转成竖屏的意思,特别要注意的是 int val = UIInterfaceOrientationPortrait; 这句代码。
一般手机APP采用的是竖屏显示,这样使用起来更加方便,但是也不是绝对,毕竟需求在不停变动,也可能在某个页面需要采取横屏显示。
以下是我踩得坑:例:在controller B 页面中使用横屏,返回上层controler A是竖屏。
在controller B页面中写入以下代码
//支持的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscapeRight;
}
//是否可以旋转
-(BOOL)shouldAutorotate{
return YES;
}
//controller即将出现的时候旋转屏幕成横屏
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//采用KVO字段控制旋转 - 好处就是审核不会被拒绝
NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
//controller即将消失时将旋转屏幕成竖屏
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
AppDelegate *appdele = (AppDelegate *)[UIApplication sharedApplication].delegate;
appdele.isSupportHori = NO;
//采用KVO字段控制旋转 - 好处就是审核不会被拒绝
NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
AppDelegate *appdele = (AppDelegate *)[UIApplication sharedApplication].delegate;
appdele.isSupportHori = YES;
}
注释:
1、isSupportHori 这个是单例,表示是否支持横屏 - 主要作用是在中途退出页面再进来的时候还是保持退出时的状态
2、orientation 采用KVO方式去强制改变,可以将键盘也弄成横屏显示。最重要的是 不会被拒绝、不会被拒绝、不会被拒绝。。。。
3、之前写的强制转屏幕使用的是iPhone私有的方法,结果:拒绝了、拒绝了、拒绝了。。。。
中途退出再进入保持横屏状态需要在APPdelegate.m中去实现:
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.isSupportHori) {
return UIInterfaceOrientationMaskLandscapeRight;
}else {
return UIInterfaceOrientationMaskPortrait;
}
}
注意: isSupportHori 在需要为横屏的页面设置为 YES,退出的时候设置成 NO,否则会保持同一种状态不变。