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强制改变物理设备方向的进阶方法
|