一、背景简介
在我们项目当中可能会遇到这样的需求,在指定页面需要指定横屏的需求,就是进入这个页面之前是不禁止横竖屏的,但是一旦跳转到这个界面的时候是需要横屏的,并且一直保持横屏,一旦离开这个界面后,又能允许横竖屏。
以下是我在简书上看到的关于此问题的解决办法,我是结合他的做法然后自己设置了一下,具体代码和截图见下。
iOS实现指定控制器的横屏禁用
二、问题解决
1>我的相关类文件的代码如下:
KODNavigationController.h代码:
#import <UIKit/UIKit.h>
@interface KODNavigationController : UINavigationController
@end
KODNavigationController.m代码:
#import "KODNavigationController.h"
@interface KODNavigationController ()
@end
@implementation KODNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (BOOL)shouldAutorotate{
if ([self.topViewController respondsToSelector:@selector(shouldAutorotate)]) {
return self.topViewController.shouldAutorotate;
}
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
if ([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
return self.topViewController.supportedInterfaceOrientations;
}
return UIInterfaceOrientationMaskAll;
}
@end
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
ViewController.m
#import "ViewController.h"
#import "KODTestViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"主控制器";
self.view.backgroundColor = [UIColor redColor];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
KODTestViewController *tvc = [[KODTestViewController alloc]init];
//[self.navigationController pushViewController:tvc animated:YES];
//TestViewController *tvc = [[TestViewController alloc]init];
[self presentViewController:tvc animated:YES completion:nil];
//[self.navigationController pushViewController:tvc animated:NO];
}
@end
KODTestViewController.h
#import <UIKit/UIKit.h>
@interface KODTestViewController : UIViewController
@end
KODTestViewController.m
#import "KODTestViewController.h"
@interface KODTestViewController ()
@property(nonatomic, weak)UIButton *btn;
@end
@implementation KODTestViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor purpleColor];
UIButton *btn = [[UIButton alloc]init];
btn.frame = [UIScreen mainScreen].bounds;
btn.center = self.view.center;
btn.backgroundColor = [UIColor orangeColor];
[btn setTitle:@"点我跳转" forState:UIControlStateNormal];
[self.view addSubview:btn];
self.btn = btn;
[btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
}
- (void)back{
[self dismissViewControllerAnimated:NO completion:nil];
}
- (BOOL)shouldAutorotate{
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
self.btn.frame = self.view.frame;
return UIInterfaceOrientationMaskPortrait;
}
@end
AppDelegate.h
#import "AppDelegate.h"
#import "KODNavigationController.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
ViewController *vc = [[ViewController alloc]init];
KODNavigationController *nav = [[KODNavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
@end
2>分析
KODNavigationController导航栏控制器的原因是有两个:
①给其设置title,能更清晰的判断屏幕方向变化
②因为如果要实现指定界面横屏的功能,只能这个界面是通过modal方式展示出来的,所以用这个控制器可以通过其push出控制器来对比效果
KODTestViewController控制器在这里的作用就是所需要指定横屏的界面
AppDelegate中设置了window的根控制器为KODNavigationController
我们通过点击ViewController控制器Modal出KODTestViewController,需要KODTestViewController只能是按照横屏方向展示
3>代码解释:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
self.btn.frame = self.view.frame;
return UIInterfaceOrientationMaskPortrait;
}
以上代码表示的是支持的横竖屏方向是横向,然后并且先设置一下frame,这样不会导致边缘白屏的问题
- (BOOL)shouldAutorotate{
return NO;
}
以上代码表示的是进入此界面后能否再转动屏幕了,返回YES,那么在此界面是可以转的,如果返回NO之后就不能旋转屏幕了。
三、文末总结
以上是能够实现我们的需求的,大家可以贴一下以上的代码试下效果。
四、扩展
iOS需要支持横竖屏
ipad支持横竖屏: