9月 2012

Android系统实现横竖屏两方向自动旋转的方法

Android系统不像iOS有shouldAutorotateToInterfaceOrientation:以及项目info.plist中的支持屏幕朝向属性设置那么直白的屏幕朝向支持,而且2.2以下和2.3以上还有对自动旋屏功能支持的差异:

在2.2一下,manifest中可以通过android:screenOrientation=”landscape”属性设置朝向,但这显然没有像iOS那样指明是landscape right还是landscape left,而且测试发现实际效果也只是锁定在了landscape的一个朝向,并不支持根据重力感应的两方向自动旋转。

在2.3及以上的target下,可以通过设置android:screenOrientation=”sensorLandscape”实现landscape的两方向自动旋转,为了同时兼容两种情况,可以采用以下的方法:

public static final int ANDROID_BUILD_GINGERBREAD = 9;
public static final int SCREEN_ORIENTATION_SENSOR_LANDSCAPE = 6;

@Override
public void onCreate(Bundle savedInstanceState)
{
     super.onCreate(savedInstanceState);
     if (Build.VERSION.SDK_INT >= ANDROID_BUILD_GINGERBREAD)
     {
         setRequestedOrientation(SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
     }
}

UIImagePickerController在iPad上应用与iPhone类设备上的应用区别以及sourceType导致的crash

之前调用UIImagePickerController显示照相机和相册时用的一直是presentModalViewController:方法直接显示,后来发现这样的用法在New iPad上会导致:
Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘On iPad, UIImagePickerController must be presented via UIPopoverController
这个错误,并且这个错误只出现在显示系统相册时,照相模式时不会提示这个错误。

按照错误字面意思来看,在iPad上UIImagePickerController必须通过UIPopoverController来显示,g了一下,发现提到相关问题的文章也有很多,替换的具体实现可以看下面的参考链接。可是奇怪的问题是直接presentModalViewController的方法实际测试在iPad2上面没有任何问题,关于这点我看到有老外提到说这个可能是UIKit的bug?!

另外,我发现在替换了UIPopoverController的实现后,打开相册时在小框里狂托时会有极大的可能性导致crash,有时候时EXC_BAD_ACCESS,有时候Collection mutated while being enumerated,还有时候提示数组0索引位置不可用…
Continue reading…

iOS6对于shouldAutorotateToInterfaceOrientation的改动以及其他一些窗口相关细节

iOS6正式版发布当天博主我就更新了,随后也更新了对应的XCode以及iOS SDK,更新到了4.5 (4G182)。然后更新原有4.4 iOS5 SDK的项目,目前最主要的发现就是iOS6对于app屏幕朝向支持以及自动旋屏时的处理方式的变动。

简而言之就是iOS6下的

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
	return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

这个不会再被调用,取而代之的是这俩个组合:

- (BOOL)shouldAutorotate
{
	return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
	return UIInterfaceOrientationMaskLandscape;
}

当然,为了保持对旧版本系统行为的兼容性,不要删掉不用的那个调用。另外还有一个这个preferred朝向也可以加上

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
	return UIInterfaceOrientationLandscapeRight;
}

当我替换完这俩个操作后尝试运行app,发现会报如下的异常:
Terminating app due to uncaught exception ‘UIApplicationInvalidInterfaceOrientation’, reason: ‘Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES’
Continue reading…