-
想要app支持屏幕旋轉(zhuǎn),info.plis文件必須勾選支持旋轉(zhuǎn)的幾個(gè)選項(xiàng):
image 控制屏幕旋轉(zhuǎn)的三個(gè)方法:
- (BOOL)shouldAutorotate;
- (UIInterfaceOrientationMask)supportedInterfaceOrientations;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
第一個(gè)方法決定是否支持多方向旋轉(zhuǎn)屏,如果返回NO則后面的兩個(gè)方法都不會(huì)再被調(diào)用,而且只會(huì)支持默認(rèn)的UIInterfaceOrientationMaskPortrait方向;
第二個(gè)方法直接返回支持的旋轉(zhuǎn)方向,該方法在iPad上的默認(rèn)返回值是UIInterfaceOrientationMaskAll,iPhone上的默認(rèn)返回值是UIInterfaceOrientationMaskAllButUpsideDown,官方文檔有說明
第三個(gè)方法返回最優(yōu)先顯示的屏幕方向,比如同時(shí)支持Portrait和Landscape方向,但想優(yōu)先顯示Landscape方向,那軟件啟動(dòng)的時(shí)候就會(huì)先顯示Landscape,在手機(jī)切換旋轉(zhuǎn)方向的時(shí)候仍然可以在Portrait和Landscape之間切換;
在UINavigationController或者自定義的UINavigationController中重寫上面三個(gè)方法,即可實(shí)現(xiàn)在特定的控制器實(shí)現(xiàn)屏幕旋轉(zhuǎn)代碼如下:
- (BOOL)shouldAutorotate{
// 返回當(dāng)前顯示的viewController是否支持旋轉(zhuǎn)
return [self.visibleViewController shouldAutorotate];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
// 返回當(dāng)前顯示的viewController支持旋轉(zhuǎn)的方向
return [self.visibleViewController preferredInterfaceOrientationForPresentation];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
// 返回當(dāng)前顯示的viewController是優(yōu)先旋轉(zhuǎn)的方向
if (![self.visibleViewController isKindOfClass:[UIAlertController class]]) {//iOS9 UIWebRotatingAlertController
return [self.visibleViewController supportedInterfaceOrientations];
}else{
return UIInterfaceOrientationMaskPortrait;
}
}
- 然后在想要旋轉(zhuǎn)屏幕的的控制器中重寫上面三個(gè)方法:
- (BOOL)shouldAutorotate {
return YES;
}
// 這里返回需要支持旋轉(zhuǎn)的方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeLeft;
}
// 優(yōu)先顯示的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
