iOS 調用系統相機默認是豎屏的,網上很多強制橫屏的方法都無效。以下代碼經測試兼容 iOS 7、8。
自定義一個 UIImagePickerController 並覆蓋以下方法:
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft;
}
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotate {
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
return YES;
} else {
return NO;
}
}
要兼容 iOS 8,還需在 AppDelegate 中加入以下代碼:
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
NSString* strSubClass = [NSString stringWithUTF8String:object_getClassName(window.rootViewController.presentedViewController)];
if ([@"ImgTakeViewController" isEqualToString:strSubClass]) {
return UIInterfaceOrientationMaskAll;
}
return [application supportedInterfaceOrientationsForWindow:window];
}