簡體   English   中英

通用應用中的iOS 6方向問題

[英]IOS 6 orientation issue in universal app

在我的通用應用程序中,我需要為iPhone和iPad處理不同的方向。 對於ipad,我需要允許橫向放置,而對於iphone肖像則需要單獨放置。 我首先返回下面的代碼

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        return UIInterfaceOrientationIsPortrait(interfaceOrientation);

    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

在IOS 5中工作正常,但在IOS 6中根本不觸發自動旋轉方法。 之后,我將方法更改為

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait;
}

甚至在IOS 6中根本沒有觸發這種方法。

我的plist設置是

在此處輸入圖片說明

我需要同時處理IOS 5和IOS 6的兩個方向[iPhone-portrait,iPad-landscape]。請指導我解決此問題。

您的rootviewcontroller是UINavigation控制器還是UITabbarcontroller? 如果是這樣,如果您在視圖控制器中調用這些方法,則這些方法將無法工作。

因此,在這些容器視圖控制器上創建一個目標C類別,並將其添加到您的項目中。

@implementation UINavigationController (autorotate)


 -(NSUInteger)supportedInterfaceOrientations
 {
   //make the check for iphone/ipad here

if(IPHONE)
    {
return UIInterfaceOrientationMaskPortrait;
    } 
else
    {
return UIInterfaceOrientationMaskLandscape;
    }

 }

 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
 {
return UIInterfaceOrientationPortrait;
 }

 - (BOOL)shouldAutorotate
 {
return NO;
 }

將此方法添加到您的應用程序委托中,使其100%工作

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  // iOS 6 autorotation fix
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationPortraitUpsideDown;
    }
    else
        return  UIInterfaceOrientationMaskAll;
}

您想要的是以下內容:

#pragma mark - iOS 5.1 and under Rotation Methods

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; {
  if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
  }
  else{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
  }
}

#pragma mark - iOS 6.0 and up Rotation Methods

- (BOOL)shouldAutorotate; {
  return YES;
}

- (NSUInteger)supportedInterfaceOrientations; {
  if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
  }
  else{
    return UIInterfaceOrientationMaskLandscape;
  }
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation; {
  if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    return UIInterfaceOrientationPortrait;
  }
  else{
    return UIInterfaceOrientationLandscapeLeft;
  }
}

您必須確保2折

首先 ,這是在RootViewController實現的。 如果要將UIViewController嵌入到UINavigationController (或UITabBarController )內部,則必須創建一個實現這些方法並使用它的自定義UINavigationController (或自定義UITabBarController )類。

其次 ,您必須確保Info.plist文件中具有受支持的方向。 否則,系統將覆蓋這些方法的返回值( 注意:您也可以通過單擊目標“摘要”頁面中的按鈕來輕松更改這些值)。

希望對您有所幫助!

這是Pradeep答案的快速版本。 您無需繼承所有導航控制器的子類即可獲得想要的工作(也就是,僅針對iPhone關閉橫向)

只需將其添加到您的應用程序委托中:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    } else {
        return Int(UIInterfaceOrientationMask.All.rawValue)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM