原文鏈接https://my.oschina.net/huqiji/blog/3031940第一種方法會出現(xiàn)無法轉(zhuǎn)屏的問題肥橙,我就沒有列出來镰绎,大家想看的話就去原文里自己去試吧为肮,我這邊用的是第二種初烘,親測有效口注,廢話不多說直接上代碼变擒。
第一種解決方案(不推薦,直接跳過看第二種解決方案):
//強(qiáng)制轉(zhuǎn)屏
- (void)interfaceOrientation:(UIInterfaceOrientation)orientation{
if([[UIDevicecurrentDevice] respondsToSelector:@selector(setOrientation:)]) {??
? ? SEL selector? =NSSelectorFromString(@"setOrientation:");
NSInvocation*invocation = [NSInvocationinvocationWithMethodSignature:[UIDeviceinstanceMethodSignatureForSelector:selector]];??
?? [invocation setSelector:selector];? ?
? ? [invocation setTarget:[UIDevicecurrentDevice]];intval = orientation;// 從2開始是因?yàn)? 1 兩個(gè)參數(shù)已經(jīng)被selector和target占用
[invocation setArgument:&val atIndex:2];??
? ? [invocation invoke];?
? }}
強(qiáng)制橫屏:
[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];
強(qiáng)制豎屏:
[self interfaceOrientation:UIInterfaceOrientationPortrait];
只在某一個(gè)界面提供轉(zhuǎn)屏的解決方法如下AppDelegate.m下操作
-(UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window {
NSLog(@"0000000---------%@",NSStringFromClass([[selftopViewController]class]));
if([NSStringFromClass([[selftopViewController]class]) isEqualToString:@"想要提供轉(zhuǎn)屏的控制器的名字"])
{
//橫屏
returnUIInterfaceOrientationMaskLandscapeRight;? ?
}
//豎屏
returnUIInterfaceOrientationMaskPortrait;
}
/獲取界面最上層的控制器
- (UIViewController*)topViewController {
return[selftopViewControllerWithRootViewController:[UIApplicationsharedApplication].keyWindow.rootViewController];
}
//一層一層的進(jìn)行查找判斷
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
if([rootViewController isKindOfClass:[UITabBarControllerclass]]) {UITabBarController* tabBarController = (UITabBarController*)rootViewController;return[selftopViewControllerWithRootViewController:tabBarController.selectedViewController];? ?
}elseif([rootViewController isKindOfClass:[UINavigationControllerclass]]) {
UINavigationController* nav = (UINavigationController*)rootViewController;return[selftopViewControllerWithRootViewController:nav.visibleViewController];? ? }elseif(rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return[selftopViewControllerWithRootViewController:presentedViewController];?
? }else{
returnrootViewController;?
? }
}
如果你的應(yīng)用的根控制器是Nav就把下面這段代碼放到Nav根控制器下疆导,如果是TabVC放到TabVC的下面
- (BOOL)shouldAutorotate{returnYES;}- (UIInterfaceOrientationMask)supportedInterfaceOrientations{returnUIInterfaceOrientationMaskPortrait;}
然后在你想橫屏的控制器加上這段代碼赁项,基本上橫屏問題就可以搞定了,前提是你的這個(gè)控制器是moda出來的澈段,如果是push的話就要使用上文提到的強(qiáng)制橫豎屏的方法悠菜,下面這段代碼是不起作用的
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return(toInterfaceOrientation ==UIInterfaceOrientationLandscapeRight);
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
returnUIInterfaceOrientationMaskLandscapeRight;
}
第二種解決方案:
靈活設(shè)置橫豎屏,不用區(qū)分Push還是Present,都是可以設(shè)置败富。
第一步:
在AppDelegate.h中添加旋轉(zhuǎn)屬性/**
* 是否允許轉(zhuǎn)向
*/@property(nonatomic,assign)BOOLallowRotation;
在AppDelegate.m中添加轉(zhuǎn)屏的代理方法
- (UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(nullableUIWindow*)window{
if(self.allowRotation ==YES)
{
//橫屏
returnUIInterfaceOrientationMaskLandscape;? ? ? ? ? ?
}else{
//豎屏
returnUIInterfaceOrientationMaskPortrait;?
? ? ? ? ? }?
? }
第二步:
設(shè)置橫豎屏的核心方法悔醋,我是直接把這個(gè)方法添加到了UIDevice的分類中,代碼如下:
UIDevice+TFDevice.h :
#import<UIKit/UIKit.h>@interfaceUIDevice(TFDevice)/**
* @interfaceOrientation 輸入要強(qiáng)制轉(zhuǎn)屏的方向
*/+ (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation;@end
UIDevice+TFDevice.m :
#import"UIDevice+TFDevice.h"
@implementationUIDevice(TFDevice)
+ (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation{
NSNumber*resetOrientationTarget = [NSNumbernumberWithInt:UIInterfaceOrientationUnknown];? ? ? ? ? ? ? ? [[UIDevicecurrentDevice] setValue:resetOrientationTarget forKey:@"orientation"];NSNumber*orientationTarget = [NSNumbernumberWithInt:interfaceOrientation];? ? ? ? ? ?
? ? [[UIDevicecurrentDevice] setValue:orientationTarget forKey:@"orientation"];?
? }
@end
第三步:
在需要設(shè)置橫屏的控制器的ViewDidLoad中添加下面代碼:
- (void)viewDidLoad {?
? [superviewDidLoad];? ?
AppDelegate * appDelegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;//允許轉(zhuǎn)成橫屏
appDelegate.allowRotation =YES;//調(diào)用橫屏代碼[UIDeviceswitchNewOrientation:UIInterfaceOrientationLandscapeRight];
}
第四步 (針對Push出的控制器來說):
需要注意的是push過去的時(shí)候變成橫屏兽叮,pop出去的時(shí)候在設(shè)置豎屏芬骄,此時(shí)最好禁用系統(tǒng)的側(cè)滑返回手勢猾愿。
-(void)viewWillAppear:(BOOL)animated{?
? [superviewWillAppear:animated];//禁用側(cè)滑手勢方法self.navigationController.interactivePopGestureRecognizer.enabled =NO;
}
-(void)viewWillDisappear:(BOOL)animated{? ?
[superviewWillDisappear:animated];//禁用側(cè)滑手勢方法self.navigationController.interactivePopGestureRecognizer.enabled =YES;
}
第五步:
push控制器:
//點(diǎn)擊導(dǎo)航欄返回按鈕的時(shí)候調(diào)用,所以Push出的控制器最好禁用側(cè)滑手勢:
AppDelegate * appDelegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;appDelegate.allowRotation =NO;//關(guān)閉橫屏僅允許豎屏//切換到豎屏[UIDeviceswitchNewOrientation:UIInterfaceOrientationPortrait];? [self.navigationController popViewControllerAnimated:YES];
present控制器:
AppDelegate * appDelegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;appDelegate.allowRotation =NO;//關(guān)閉橫屏僅允許豎屏//切換到豎屏[UIDeviceswitchNewOrientation:UIInterfaceOrientationPortrait];? ? [selfdismissViewControllerAnimated:YEScompletion:nil];
第六步: 上圖