iOS强制横屏+键盘横屏显示+中途退出再进入也是横屏

22 篇文章 0 订阅

一般手机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、



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值