最近开始做和视频类相关的项目,实现在UINavigationController中UIViewController的视频横竖屏需求,下面是我的代码。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
PLMediaMasterViewController *homeViewController = [[PLMediaMasterViewController alloc] init];
self.navController = [[CunstomNavViewController alloc] initWithRootViewController:homeViewController];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
return YES;
}
#import "CunstomNavViewController.h"//导航控制器
//支持旋转
-(BOOL)shouldAutorotate{
return [self.topViewController shouldAutorotate];
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return [self.topViewController supportedInterfaceOrientations];
}
********************************************
#import "PLMediaMasterViewController.h"//竖屏页面
//支持旋转
-(BOOL)shouldAutorotate{
return YES;
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (IBAction)longhuZhibo:(UIButton *)sender {
//点击进入横屏页面
PLMediaChiefViewController *controller = [[PLMediaChiefViewController alloc] init];
//self.navController,这个应该是一个全局的对象
[self.navController presentViewController:controller animated:YES completion:^{
}];
}
****************************************
#import "PLMediaChiefViewController.h"//横屏页面
**注意的事项,横屏页面Windows的frame会发生变化,自己的view不会变化,在横屏中如果需要设置UI的frame需要放到viewWillAppear中
因为进入VC中首先执行viewDidLoad方法,然后执行支supportedInterfaceOrientations 和shouldAutorotate方法,最后执行viewWillAppear方法,
只有执行完supportedInterfaceOrientations之后,Windows的frame才会发生变化。
//支持旋转
-(BOOL)shouldAutorotate{
return YES;
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
if (横屏) {
return UIInterfaceOrientationMaskLandscapeRight;
}else{
return UIInterfaceOrientationMaskPortrait;
}
}
//一开始的方向 很重要
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
if (横屏) {
return UIInterfaceOrientationLandscapeRight;
}else{
return UIInterfaceOrientationPortrait;
}
}
**如有好想法,留言沟通🤝🤝