iOS 6横竖屏切换

本文深入解析了iOS设备上UIViewController之间的横竖屏切换机制,并提供了实例代码来帮助开发者理解如何在不同方向下正确实现界面的适配。通过设置shouldAutorotateToInterfaceOrientation:和supportedInterfaceOrientations:函数,开发者可以确保应用在各种屏幕方向下都能正常运行,避免出现方向切换时的不兼容问题。
摘要由CSDN通过智能技术生成

iOS6.0版本之前,UIViewController之间的横竖屏切换,只需设置一个函数:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation


UIInterfaceOrientation是UIApplication.h头文件中定义的枚举类型:


typedefNS_ENUM(NSInteger, UIInterfaceOrientation) {

    UIInterfaceOrientationPortrait           =UIDeviceOrientationPortrait,//竖屏home键在下方

    UIInterfaceOrientationPortraitUpsideDown =UIDeviceOrientationPortraitUpsideDown,//竖屏home键在上方

    UIInterfaceOrientationLandscapeLeft      =UIDeviceOrientationLandscapeRight,//横屏home键在右方

    UIInterfaceOrientationLandscapeRight     =UIDeviceOrientationLandscapeLeft//横屏home键在左方

};

横竖屏一共4个方向,只需在shouldAutorotateToInterfaceOrientation:方法中返回相应的结果,如需要竖屏home键在下方,则:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

{

    // Return YES for supported orientations

   return (toInterfaceOrientation ==UIInterfaceOrientationPortrait);

}

return YES;将支持所有方向。若要支持多种方向,可以这样设置:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

{

    // Return YES for supported orientations

    return (toInterfaceOrientation == UIInterfaceOrientationPortrait||toInterfaceOrientation == UIDeviceOrientationPortraitUpsideDown);

}


iOS6.0版本以后,UIViewController之间的横竖屏切换,需多设置一个函数:

-(NSUInteger)supportedInterfaceOrientations;

返回UIInterfaceOrientationMask枚举类型,

UIInterfaceOrientationMask也是定义在UIApplication.h头文件中:

typedefNS_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),

};


除了设置shouldAutorotateToInterfaceOrientation:之外,还要将supportedInterfaceOrientations返回的方向与shouldAutorotateToInterfaceOrientation:保持一致。如:Portrait

-(NSUInteger)supportedInterfaceOrientations{

    

    returnUIInterfaceOrientationMaskPortrait;//UIInterfaceOrientationMaskLandscapeUIInterfaceOrientationMaskAllUIInterfaceOrientationMaskAllButUpsideDown

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

   return (interfaceOrientation ==UIInterfaceOrientationPortrait);

}


应用场景:cotrollerA只支持竖屏显示,controllerB只支持横屏显示,在从cotrollerA切换到controllerB,或从controllerB切换到controllerA的时候,为了兼容ios6版本,在controllerA中:

-(NSUInteger)supportedInterfaceOrientations{

    

    return UIInterfaceOrientationMaskPortrait;//UIInterfaceOrientationMaskLandscapeUIInterfaceOrientationMaskAllUIInterfaceOrientationMaskAllButUpsideDown

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

controllerB中

-(NSUInteger)supportedInterfaceOrientations{

    

    return UIInterfaceOrientationMaskLandscapeRight;//UIInterfaceOrientationMaskLandscapeUIInterfaceOrientationMaskAllUIInterfaceOrientationMaskAllButUpsideDown

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);

}


否则,切换过程中,会出现竖屏变横屏,横屏变竖屏的情况。

最重要的一点,一定要确保你设置的方向包含在下面:


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值