第一种方案(不推荐,直接跳过看第二种方案):
-,需要:
强制横竖屏,在某些情况下很重要,我在网上找了很多解决方法,但都没有找到解决方法,请原谅我有点笨,但纠结了一段时间,终于解决了这个问题,会出现不能转屏!!!
//强制转屏
- (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 两个参数已经被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 更新:
在设置控制器旋转画面时,我当前应用中的处理方式:
因为在大多数场景下,只有少数页面是横屏的,所以基本上是竖屏的,所以我在处理这两种情况的时候,我比较喜欢使用系统提供的方法:
如果你的应用的根控制器是Nav,把下面的代码放在Nav根控制器下,如果是TabVC,就放在TabVC下
- (BOOL)shouldAutorotate{
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
然后将此代码添加到您要横屏的控制器中。基本可以解决横屏问题。前提是你的控制器来自 moda。如果是推,则必须使用上述力量。横竖屏的方法,下面的代码不起作用
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeRight;
}
第二种解决方案:
灵活设置横竖屏安卓强制横屏软件下载,无需区分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出来的时候设置竖屏。这时候最好禁用系统的侧滑返回手势。
-(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 步:
演示地址:
发表评论