【Xamarin挖墙脚系列:IOS-关于手机支持的屏幕方向】

设置支持的屏幕方向有两个级别,一个是app级别的,另一个是viewController级别的。

 

app 级别的可以在[target]-[general]-[device orientation]里面设置,

 

默认情况下Upside Down没有勾选,其他都勾选了。

(为什么Upside Down不推荐勾选呢,因为iPhone的电话app是不支持Upside Down的,如果你的app支持Upside Down,万一用户在用你的app的时候Upside Down了,这时候来了电话,就会看到整个来电的画面是颠倒的,用户体验很不好。一向注重用户体验的苹果是不推荐你勾选Upside Down的)

 

viewController级别的就是在各个viewController里面设置了。

这里有一点需要注意,viewController的设置受app级别设置的限制,也就是viewController能够设置的屏幕方向只能是在app级别中勾选的一种或多种,没有勾选的是不能设置的。比如上面的Upside Down没有勾选,那么viewController也就不能设置Upside Down的方向。

 

那么在viewController里面怎么设置屏幕方向呢?

iOS6以前:

 

// 设置屏幕只支持竖向
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

从iOS6开始,上面的方法已经被抛弃,有了3个新的方法:
 

 

// 不支持屏幕旋转
- (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类型。

 

posted @ 2016-03-05 21:13  特洛伊-Micro  阅读(379)  评论(0编辑  收藏  举报