一般手机APP采用的是竖屏显示,这样使用起来更加方便,但是也不是绝对,毕竟需求在不停变动,也可能在某个页面需要采取横屏显示。
以下是我踩得坑:例:在controller B 页面中使用横屏,返回上层controler A是竖屏。
在controller B页面中写入以下代码
//支持的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
returnUIInterfaceOrientationMaskLandscapeRight;
}
//是否可以旋转
-(BOOL)shouldAutorotate{
returnYES;
}
//controller即将出现的时候旋转屏幕成横屏
-(void)viewWillAppear:(BOOL)animated
{
[superviewWillAppear:animated];
//采用KVO字段控制旋转 -好处就是审核不会被拒绝
NSNumber *orientationUnknown = [NSNumbernumberWithInt:UIInterfaceOrientationUnknown];
[[UIDevicecurrentDevice] setValue:orientationUnknownforKey:@"orientation"];
NSNumber *orientationTarget = [NSNumbernumberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevicecurrentDevice] setValue:orientationTargetforKey:@"orientation"];
}
//controller即将消失时将旋转屏幕成竖屏
-(void)viewWillDisappear:(BOOL)animated
{
[superviewWillDisappear:animated];
AppDelegate *appdele = (AppDelegate *)[UIApplicationsharedApplication].delegate;
appdele.isSupportHori =NO;
//采用KVO字段控制旋转 -好处就是审核不会被拒绝
NSNumber *orientationUnknown = [NSNumbernumberWithInt:UIInterfaceOrientationUnknown];
[[UIDevicecurrentDevice] setValue:orientationUnknownforKey:@"orientation"];
NSNumber *orientationTarget = [NSNumbernumberWithInt:UIInterfaceOrientationPortrait];
[[UIDevicecurrentDevice] setValue:orientationTargetforKey:@"orientation"];
}
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
AppDelegate *appdele = (AppDelegate *)[UIApplicationsharedApplication].delegate;
appdele.isSupportHori =YES;
}
注释:
1、isSupportHori这个是单例,表示是否支持横屏 - 主要作用是在中途退出页面再进来的时候还是保持退出时的状态
2、orientation采用KVO方式去强制改变,可以将键盘也弄成横屏显示。
3、

本文介绍了一种在iOS应用中实现特定页面横屏显示的方法,包括如何设置支持的方向、是否允许自动旋转,以及如何在视图出现和消失时进行屏幕方向的切换。
1万+

被折叠的 条评论
为什么被折叠?



