您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 相機界面強制橫屏的解法

編輯:iOS 開發綜合

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];
}

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved