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;//UIInterfaceOrientationMaskLandscape、UIInterfaceOrientationMaskAll、UIInterfaceOrientationMaskAllButUpsideDown
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation ==UIInterfaceOrientationPortrait);
}
应用场景:cotrollerA只支持竖屏显示,controllerB只支持横屏显示,在从cotrollerA切换到controllerB,或从controllerB切换到controllerA的时候,为了兼容ios6版本,在controllerA中:
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;//UIInterfaceOrientationMaskLandscape、UIInterfaceOrientationMaskAll、UIInterfaceOrientationMaskAllButUpsideDown
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
在controllerB中 :
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscapeRight;//UIInterfaceOrientationMaskLandscape、UIInterfaceOrientationMaskAll、UIInterfaceOrientationMaskAllButUpsideDown
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
否则,切换过程中,会出现竖屏变横屏,横屏变竖屏的情况。
最重要的一点,一定要确保你设置的方向包含在下面: