第一種方案(不推薦,直接跳過看第二種方案):
-,需要:
強制橫豎屏,在某些情況下很重要,我在網(wǎng)上找了很多解決方法,但都沒有找到解決方法,請原諒我有點笨,但糾結了一段時間,終于解決了這個問題,會出現(xiàn)不能轉屏?。?!
//強制轉屏
- (void)interfaceOrientation:(UIInterfaceOrientation)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 = orientation;
// 從2開始是因為0 1 兩個參數(shù)已經(jīng)被selector和target占用
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
勢力景觀:
[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];
強制豎屏:
[self interfaceOrientation:UIInterfaceOrientationPortrait];
終于解決了這個頭疼的問題,哈哈哈---啊---duang
在某個界面只提供屏幕旋轉的解決方案如下。
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
NSLog(@"0000000---------%@",NSStringFromClass([[self topViewController] class]));
if ([NSStringFromClass([[self topViewController] class]) isEqualToString:@"想要提供轉屏的控制器的名字"]) {
//橫屏
return UIInterfaceOrientationMaskLandscapeRight;
}
//豎屏
return UIInterfaceOrientationMaskPortrait;
}
//獲取界面最上層的控制器
- (UIViewController*)topViewController {
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
//一層一層的進行查找判斷
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* nav = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:nav.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
return rootViewController;
}
}
2017.5.15 更新:
在設置控制器旋轉畫面時,我當前應用中的處理方式:
因為在大多數(shù)場景下,只有少數(shù)頁面是橫屏的,所以基本上是豎屏的,所以我在處理這兩種情況的時候,我比較喜歡使用系統(tǒng)提供的方法:
如果你的應用的根控制器是Nav,把下面的代碼放在Nav根控制器下,如果是TabVC,就放在TabVC下
- (BOOL)shouldAutorotate{
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
然后將此代碼添加到您要橫屏的控制器中?;究梢越鉀Q橫屏問題。前提是你的控制器來自 moda。如果是推,則必須使用上述力量。橫豎屏的方法,下面的代碼不起作用
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeRight;
}
第二種解決方案:
靈活設置橫豎屏安卓強制橫屏軟件下載,無需區(qū)分Push與否安卓強制橫屏軟件下載,均可設置。
第一步:
在AppDelegate.h中添加旋轉屬性
/**
* 是否允許轉向
*/
@property(nonatomic,assign)BOOL allowRotation;
在AppDelegate.m中添加轉屏的代理方法
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
{
if (self.allowRotation == YES) {
//橫屏
return UIInterfaceOrientationMaskLandscape;
}else{
//豎屏
return UIInterfaceOrientationMaskPortrait;
}
}
第2步:
設置橫豎屏的核心方法,我直接把這個方法加到里面,代碼如下:
+.h:
#import
@interface UIDevice (TFDevice)
/**
* @interfaceOrientation 輸入要強制轉屏的方向
*/
+ (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation;
@end
+.m:
#import "UIDevice+TFDevice.h"
@implementation UIDevice (TFDevice)
+ (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation
{
NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:interfaceOrientation];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
@end
第三步:
在需要設置橫屏的控制器中加入如下代碼:
- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//允許轉成橫屏
appDelegate.allowRotation = YES;
//調用橫屏代碼
[UIDevice switchNewOrientation:UIInterfaceOrientationLandscapeRight];
}
第四步(對于推送控制器):
需要注意的是,push過去的時候變成橫屏,pop出來的時候設置豎屏。這時候最好禁用系統(tǒng)的側滑返回手勢。
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//禁用側滑手勢方法
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
//禁用側滑手勢方法
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
第五步:
推送控制器:
//點擊導航欄返回按鈕的時候調用,所以Push出的控制器最好禁用側滑手勢:
AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.allowRotation = NO;//關閉橫屏僅允許豎屏
//切換到豎屏
[UIDevice switchNewOrientation:UIInterfaceOrientationPortrait];
[self.navigationController popViewControllerAnimated:YES];
控制器:
AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.allowRotation = NO;//關閉橫屏僅允許豎屏
//切換到豎屏
[UIDevice switchNewOrientation:UIInterfaceOrientationPortrait];
[self dismissViewControllerAnimated:YES completion:nil];
第 6 步:
演示地址: