一、
在project->general,只选择竖屏方法,如下图所示:
不重载
- (UIInterfaceOrientationMask)supportedInterfaceOrientations;
- (BOOL)shouldAutorotate;
这两个方法,系统会在WKwebview播放视频的时候,当手机也是横屏的话,播放视频的VC会自动横屏(WKwebview不横屏,)。
二、
某些App会按照下面的方法设置App竖屏。
1、在project->general,选择所有方法,如下图所示:
2、加一个UIViewController 的Category来控制竖屏,这样方便适配需要横屏的VC。
这时候需要在Category增加对AVPlayerViewController的强制横屏,即可实现播放视频横屏(WKwebview不横屏)。
简单代码如下:
#import "UIViewController+CYHBase.h"
#import "MethodSwizzling.h"
#import <objc/runtime.h>
@interface UIViewController ()
@end
@implementation UIViewController(CYHBase)
- (void)setOrgNavigationController:(UINavigationController*)navigationController
{
objc_setAssociatedObject(self, @"orgNavigationController", navigationController, OBJC_ASSOCIATION_ASSIGN);
}
- (UINavigationController *)orgNavigationController
{
return objc_getAssociatedObject(self, @"orgNavigationController");
}
+ (void)load
{ [UIViewControllerswizzleMethod:@selector(viewDidLoad)withMethod:@selector(cyhBaseViewDidLoad)];
[UIViewControllerswizzleMethod:@selector(viewWillAppear:)withMethod:@selector(cyhBaseViewWillAppear:)]; [UIViewControllerswizzleMethod:@selector(viewWillDisappear:)withMethod:@selector(cyhBaseViewWillDisappear:)];
[UIViewControllerswizzleMethod:@selector(supportedInterfaceOrientations)withMethod:@selector(cyhBaseSupportedInterfaceOrientations)];
}
-(void)cyhBaseViewDidLoad{
[self cyhBaseViewDidLoad];
}
- (void)cyhBaseViewWillDisappear:(BOOL)animated{
[self cyhBaseViewWillDisappear:animated];
}
- (void)cyhBaseViewWillAppear:(BOOL)animated{
/// 手势返回适配iOS9
[self cyhBaseViewWillAppear:animated];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate{
return YES;
}
- (UIInterfaceOrientationMask)cyhBaseSupportedInterfaceOrientations{
if([NSStringFromClass([self class]) isEqualToString:@"AVPlayerViewController"])
{
//视频webview播放视频的时候,横着全屏
return UIInterfaceOrientationMaskLandscapeRight;
}
return [self cyhBaseSupportedInterfaceOrientations];
}
上两种方法没在UIWebView上测试,只敢确认在WKwebview有效。
三、
适用UIWebView\WKwebview
1、AppDelaget.h
/*** 是否允许横屏的标记 */
@property (nonatomic,assign)BOOL allowRotation;
2、AppDelaget.m
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.allowRotation) {
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
3、webViewController
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainFullScreen) name:UIWindowDidBecomeVisibleNotification object:nil];//进入全屏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];//退出全屏
#pragma - mark 进入全屏
-(void)begainFullScreen{
if(!self.didWebViewLoadOK) {
return;
}
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = YES;
[[UIDevice currentDevice] setValue:@"UIInterfaceOrientationLandscapeLeft" forKey:@"orientation"];
//强制转屏:
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = UIInterfaceOrientationLandscapeLeft;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
#pragma - mark 退出全屏
-(void)endFullScreen
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = NO;
//强制归正:
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val =UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
但方法三会造成webview也会转屏,在视觉效果上体验不好。
第三种方法参考工程 iOS_Demo/02-UIWebview at master · darren90/iOS_Demo · GitHub