iOS之横竖屏小结

需求:项目要求是只支持竖屏的,但是视屏播放需要支持左右横屏。

首先,工程文件 -> General -> Device Orientation
选择 Portrait(正常竖屏)、Landscape Left (左横屏)、Landscape Right(右横屏);

这里写图片描述

如果在创建项目时,选择了 Devices:Universal,那么打开 info.plist 文件应该可以看到:

这里写图片描述

这里是配置横竖屏信息的地方,如果有不合适的地方可以进行调整。

然后,找到项目的根控制器,设置不支持屏幕旋转,并设置默认方向为正常反向。设置后会发现,凡是添加到根控制器的视图控制器都不再支持左右横屏。

方案一:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
    return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;//只支持这一个方向(正常的方向)
}

/**重点:
根控制器的位置:
1.如果在UIViewController上,则在UIViewController对应的.m文件中加入三个函数即可。
2.如果在UITabBarController上,则在UITabBarController对应的.m文件中加入三个函数即可。
3.如果在UINavigationController上,则在UINavigationController对应的.m文件中加入三个函数即可。

(不按情况执行,问题无法解决)
*/

其中,第一个方法可以省略,因为在iOS 6.0后这个方法就被废弃了:

// Applications should use supportedInterfaceOrientations and/or shouldAutorotate..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0) __TVOS_PROHIBITED;

3、然后,实现MPMoviePlayerViewController的子类,并在 .m 文件中实现以下方法,就能实现需求了。

- (BOOL)shouldAutorotate
{
    return Yes;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}

方案二:

以导航控制器为跟控制器为例:

#pragma 横竖屏
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    if ([self.topViewController isKindOfClass:[MPMoviePlayerViewController class]] || [self.presentedViewController isKindOfClass:[MPMoviePlayerViewController class]]) { 
        return YES;
    }
    return (toInterfaceOrientation == UIInterfaceOrientationMaskPortrait);
}
- (BOOL)shouldAutorotate
{
    if ([
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值