IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> iOS混合开发支持横竖屏转换问题汇总 -> 正文阅读

[移动开发]iOS混合开发支持横竖屏转换问题汇总

1、wkwebview支持横屏

单页面支持旋转参考1参考2参考3

# pragma mark 支持横竖屏幕转换
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
     NSLog(@"UIViewController will rotate to Orientation: %ld", toInterfaceOrientation);
     
     if(([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)){//横屏
          NSLog(@"横屏");
          [self transformWkWebViewFrame];
          
     }else{//竖屏
          NSLog(@"竖屏");
          [self transformWkWebViewFrame];
     }
}
// 进入其他页面改变方向后,返回当前页面刷新布局
-(void)refreshWkWebViewFrame {
     NSInteger  width=self.view.frame.size.width;
     NSInteger  height=self.view.frame.size.height;
     
     self.wkWebView.frame = CGRectMake(0, 0, width, height);
}
// 根据旋转方向改变布局
-(void)transformWkWebViewFrame {
     NSInteger  width=self.view.frame.size.width;
     NSInteger  height=self.view.frame.size.height;
     self.wkWebView.frame = CGRectMake(0, 0, height, width);
}

当进入其他页面翻转屏幕后返回该页面要刷新当前页面frame布局

- (void)viewWillAppear:(BOOL)animated
{
     [super viewWillAppear:animated];
     [self refreshWkWebViewFrame];
}

其他要注意的,如果导航栏出现黑线框,布局时需要动态判断状态栏高度

CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
if(([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)){//横屏
    NSLog(@"横屏");
   _wkWebView = [[WKWebView alloc]initWithFrame:CGRectMake(0.0f, rectStatu
rectStatus.size.height-44)];

}else{//竖屏
    NSLog(@"竖屏");
    _wkWebView = [[WKWebView alloc]initWithFrame:CGRectMake(0.0f, rectStatus.size.height+20, self.view.frame.size.width, self.view.frame.size.height-rectStatus.size.height-20)];
}

2、特定页面不支持横屏

方案1
//支持的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
     return UIInterfaceOrientationMaskPortrait;
}
//是否可以旋转
-(BOOL)shouldAutorotate
{
     return false;
}

代码改变app不同场景的设备旋转方向支持(可以在不同的viewController中处理对设备方向的不同需求)
supportedInterfaceOrientations只能在根控制器中生效,即在navigationController或者tabbarController中生效

上述方法如果是导航子页面局限性很大,建议使用方案2

方案2

AppDelegate.h

@property (nonatomic,assign)NSInteger allowRotate; 

AppDelegate.m

//此方法会在设备横竖屏变化的时候调用
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
 
 //   NSLog(@"方向  =============   %ld", _allowRotate);
    if (_allowRotate == 1) {
        return UIInterfaceOrientationMaskAll;
    }else{
        return (UIInterfaceOrientationMaskPortrait);
    }
}
 
 
// 返回是否支持设备自动旋转
- (BOOL)shouldAutorotate
{
    if (_allowRotate == 1) {
        return YES;
    }
    return NO;
}

需要横屏转换的viewC设置

- (void)viewWillAppear:(BOOL)animated
{
     //在视图出现的时候,将allowRotate改为1,
         AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
         delegate.allowRotate = 1;
     
     [super viewWillAppear:animated];
     [self.navigationController setNavigationBarHidden:YES animated:YES];
     
     [self refreshWkWebViewFrame];
}

不需要横屏转换的viewC设置

- (void)viewWillAppear:(BOOL)animated
{
     //在视图出现的时候,将allowRotate改为1,
         AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
         delegate.allowRotate = 0;
}
- (void)viewDidLoad {
    self.title = @"扫一扫";
    [super viewDidLoad];
    
     [[WBQmapHandle sharedQmapHandle] forceToOrientation:UIInterfaceOrientationPortrait];
 
}
/*******************************************设置项目旋转方向****************************************/
- (void)forceToOrientation:(int)orientation
{
    NSNumber *orientationUnknown = [NSNumber numberWithInt:0];
    [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
    
    NSNumber *orientationTarget = [NSNumber numberWithInt:orientation];
    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}

forceToOrientation是强制旋转iOS设备物理方向,详细参考iOS强制改变物理设备方向的进阶方法

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-07-13 17:34:48  更:2021-07-13 17:36:26 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/28 1:55:38-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码