设置支持的屏幕方向有两个级别,一个是app级别的,另外一个是viewController级别的。html
app 级别的能够在[target]-[general]-[device orientation]里面设置,app
默认状况下Upside Down没有勾选,其余都勾选了。ide
(为何Upside Down不推荐勾选呢,由于iPhone的电话app是不支持Upside Down的,若是你的app支持Upside Down,万一用户在用你的app的时候Upside Down了,这时候来了电话,就会看到整个来电的画面是颠倒的,用户体验很很差。一贯注重用户体验的苹果是不推荐你勾选Upside Down的)post
viewController级别的就是在各个viewController里面设置了。url
这里有一点须要注意,viewController的设置受app级别设置的限制,也就是viewController可以设置的屏幕方向只能是在app级别中勾选的一种或多种,没有勾选的是不能设置的。好比上面的Upside Down没有勾选,那么viewController也就不能设置Upside Down的方向。htm
那么在viewController里面怎么设置屏幕方向呢?blog
iOS6之前:文档
// 设置屏幕只支持竖向
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}get
从iOS6开始,上面的方法已经被抛弃,有了3个新的方法:
it
// 不支持屏幕旋转
- (BOOL)shouldAutorotate
{
return NO;
}
// 只支持竖向
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationPortrait;
}
// 画面一开始加载时就是竖向
// - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
// return UIInterfaceOrientationPortrait;
// }
若是iOS6以前的版本也对应,那么被抛弃的那个方法也须要加上去。
可是,iOS8.3开始,在有UIAlertView的viewController里面,弹出UIAlertView的时候会崩溃,Log信息以下:
Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation',
reason: 'Supported orientations has no common orientation with the application,
and [_UIAlertShimPresentingViewController shouldAutorotate] is returning YES'
经过查阅官方文档,发现supportedInterfaceOrientations方法的返回值是UIInterfaceOrientationMask类型的,因此应该用UIInterfaceOrientationMaskPortrait。UIInterfaceOrientationMask类型从iOS6就有了,只不过到iOS8.3才会崩溃。
至于preferredInterfaceOrientationForPresentation方法,返回值仍是老的UIInterfaceOrientation类型。