iOS实现屏幕旋转有两种方式

1. 应用本身支持

ios7屏幕旋转代码 屏幕旋转ios怎么设置_屏幕旋转

2. 手动旋转UIView (这种方式我没找到旋转 系统控件的方法 如:键盘、导航、UIAlertView)

 

如果你只是要把竖屏的播放器,做成支持横屏的,没有其他界面操作, 就可以考虑用第二种方式去做,比较简单 ,不过要注意计算view Frame

这两种方式看你具体的使用场景了,具体场景选择合适的方式。

公司项目中有几个界面要支持横竖屏,(直播录制界面、直播观看界面、视频回看界面)。 刚开始我想着用第二种方式去解决,但是我们视频录制、观看界面有输入框,需要调用键盘,没找到可以手动旋转键盘的方式,就改为让应用本身支持屏幕旋转。具体做法如下:

我们项目有一个 UITabBarController ,它有3个子CustomNavigationController, 我写了一个 

CustomNavigationController继承自 UINavigationController 

1. tabBarController 中 

- (BOOL)shouldAutorotate{
returnself.selectedViewControllershouldAutorotate];
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
returnself.selectedViewControllersupportedInterfaceOrientations];
}
//初始方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
returnUIInterfaceOrientationPortrait;
}
2. CustomNavigationController 重写父类方法
- (BOOL)shouldAutorotate
{
returnNO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
returnUIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
returnUIInterfaceOrientationPortrait;
}
3. 我的支持屏幕旋转的3个ViewController是presentViewController出来的; 在支持旋转的界面加入以下代码:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
returnUIDeviceOrientationPortraitUpsideDown;
}
- (BOOL)shouldAutorotate
{
if) {
returnYES;
    }
returnNO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
if) {
returnUIInterfaceOrientationMaskPortrait;
    }
returnUIInterfaceOrientationMaskAllButUpsideDown;
}

还要做一件事---- 刷新UI布局;

4.

在ViewDidLoad中加入监听屏幕旋转的事件:

NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotificationobject:nil];

在orientChange:方法中修改UI布局  ;

(注意: 看一下屏幕尺寸的变化 [[UIScreen mainScreen] bounds].size )

 

如果要控制CustomNavigationController push到的viewController的旋转,那么就在CustomNavigationController里面区分是哪个viewController,以便单独控制!

- (BOOL)shouldAutorotate
{
UIViewController *control = self.topViewController;
ifisKindOfClass:[@"支持旋转的ViewController" class]]) {
returnshouldAutorotate];
    }
returnYES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
UIViewControllerself.topViewController;
ifisKindOfClass:[@"支持旋转的ViewController" class]]) {
returnsupportedInterfaceOrientations];
    }
returnUIInterfaceOrientationMaskPortrait;
}