ios的UIViewController中设置强制横屏

在iOS应用中,仅让WebView加载的签字页面强制横屏,其他页面保持竖屏。设置【General】的【Device Orientation】为竖屏,并在ViewController的`viewDidLoad`中监听通知。通过自定义属性`canRotate`和`isLandscape`控制屏幕方向。在接收到H5通知后调整屏幕方向。若设备未实际横置,页面不会横屏。解决该问题需要在设备旋转前设置横屏。测试过程中遇到程序因`supportedInterfaceOrientations`方法崩溃的问题,尚未找到原因。
摘要由CSDN通过智能技术生成

最近有个需求需要在webview加载的一个签字页面设置强制横屏,因为如果竖屏签字的话,名字是从上到下的,这里总结一下相关的知识和遇到的问题。

因为整个app里面只有这个签字页面需要设置为横屏,别的页面都必须是竖屏的,所以【General】 中的【Device Orientation】设置为只支持竖屏这里写图片描述

然后在Controller的viewDidLoad中注册通知
//注册Device Orientation通知
self.canRotate = NO;
self.isLandscape = NO;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];

这里用到了两个自己定义的属性canRotate和isLandscape,分别表示是否允许改变朝向和是否横屏,收到通知以后的方法如下:

- (void)deviceOrientationDidChange
{
    if (_canRotate) {
        if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait && !_isLandscape) {
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
            [self orientationChange:NO];
            //注意: UIDeviceOrientationLandscapeLeft 与 UIInterfaceOrientationLandscapeRight
        } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft &&_isLandscape) {
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
            [self orientationChange:YES];
        }
    }
}
- (void)orientationChange:(BOOL)landscapeRight
{
    _canRotate = NO;
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    if (landscapeRight) {
        [
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值