iOS开发—应用屏幕横竖屏切换设置

一、如果要项支持屏幕横竖屏切换,首先要确保程序的总开关开启,勾选所要支持的Device Orientation。注意如果这里没有勾选,代码里面再怎么设置都是不会实现横竖屏转换的。


二、假设应用需要支持正竖屏幕/左横屏/右横屏,即对1、3、4项打勾选。这样之后如果程序中的ViewController使用的是系统默认的导航栏控制器,即UINavagationController,那么就能够实现屏幕切换了。

三、如果程序中使用的是第三方库提供的(或者是自己写的)导航栏控制器,这个导航栏控制器继承UINavagationController,如果不重写其横竖屏方面的代码,那么效果就和UINavagationController,同样是支持横竖屏切换的。

四、对于非系统导航栏控制器,要想对横竖屏切换进行控制,需要添加以下两个方法(这两个方法继承自UINavagationController):

//支持的屏幕方向
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}


//是否允许改变横竖屏(开关)
- (BOOL)shouldAutorotate
{
    return YES;
}

1、

- (NSUInteger)supportedInterfaceOrientations
该方法返回所要支持的屏幕方向

typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
};

横竖屏常量定义在该枚举中,需要注意的是ios6.0之后才适用。6.0之前有另外的枚举常量可支持


2、

- (BOOL)shouldAutorotate

该方法相当于开关,如想要支持,return YES,反之为NO。


可想而知,UINavagationController中返回的值为YES。



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值