2012-10-04 50 views
5

當將我的應用程序更新爲iOS6標準時,人像/風景消失了。當我使用Xcode 3編譯時,Ir完美工作。但是現在使用最新的Xcode和最新的SDK,旋轉消失了,它始終處於縱向模式。無論我在「支持的接口方向」中輸入什麼內容。我之前用於旋轉的代碼似乎完全沒有效果。 我有這些線。iOS6中的人像和風景模式

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
{ 
    switch (toInterfaceOrientation) { 
     case UIInterfaceOrientationPortrait: 
     case UIInterfaceOrientationLandscapeLeft: 
     case UIInterfaceOrientationLandscapeRight: 
      return YES; 
     default: 
      return NO; 
    } 
} 

如何更改以及如何更改以使其重新工作?

+0

試試這個。它爲我工作: http://stackoverflow.com/questions/10096672/uiwindows-root-view-controller-does-not-rotate-to-landscape-at-app-launch –

回答

17

首先,在AppDelegate中寫下這個。這是非常IMP

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    return (UIInterfaceOrientationMaskAll); 
} 

那麼,對於UIViewControllers,其中只需要縱向模式,寫這些功能

- (BOOL)shouldAutorotate 
{ 
    return YES; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return (UIInterfaceOrientationMaskPortrait); 
} 

對於UIViewControllers,需要景觀也變化屏蔽所有。

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return (UIInterfaceOrientationMaskAllButUpsideDown); 
    //OR return (UIInterfaceOrientationMaskAll); 
} 

現在,如果您想在方向更改時進行一些更改,請使用此功能。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 

} 

編輯:

在很大程度上取決於與控制器UIViewController中嵌入

例如,如果其內部的UINavigationController,那麼你可能需要繼承的是UINavigationController的覆蓋方向。像這樣的方法。

subclassed UINavigationController(層次結構的頂級視圖控制器將控制方向。)將它設置爲self.window.rootViewController。

- (BOOL)shouldAutorotate 
{ 
    return self.topViewController.shouldAutorotate; 
} 
- (NSUInteger)supportedInterfaceOrientations 
{ 
    return self.topViewController.supportedInterfaceOrientations; 
} 

從iOS 6中,它被賦予的是UINavigationController的不會要求其UIVIewControllers用於定向支持。因此我們需要對它進行子類化。

+0

看起來像一個很好的方法但我很抱歉地說,無論我按下按鈕,支持的接口方向,還是不支持,都無法在我的舊項目中工作。而在一個新的項目中,它似乎也不起作用。不知道我在這裏做錯了什麼。 –

+0

檢查編輯答案。實際上,我也遇到了類似的問題,這個問題已經得到解決。 – mayuur

+0

將測試你說的話。但與此同時,我得到了旋轉工作將這些行設置爲委託,self.window.rootViewController = self.navigationController;並根據「按鈕選擇」旋轉工作,但如何使一些ViewController總是在肖像我不明白 –

1

不知道你的問題是否與我一樣,但狀態欄的方向正確(橫向),並且描述了UIViewController。 didFinishLaunchingWithOptions: 我在應用程序委託申請改變以下行

//[window addSubview:navigationController.view]; 

self.window.rootViewController = navigationController; 

蘋果=>此成本覈算我一天半的時間找出來,和很多錢!

6

如何支撐一個或多個景點的控制器中的應用程序,是肖像主要在iOS6的:

1)中的AppDelegate

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
    { 
     UINavigationController* ns = (UINavigationController*)self.window.rootViewController; 
     if (ns) { 
      UIViewController* vc = [ns visibleViewController]; 
//by this UIViewController that needs landscape is identified 
      if ([vc respondsToSelector:@selector(needIos6Landscape)]) 
       return [vc supportedInterfaceOrientations]; 

     } 
     return UIInterfaceOrientationMaskPortrait; //return default value 
    } 

2)中的UIView控制器(s)表示,需要橫向(或縱向+ lanscape等):

//flag method 
-(void)needIos6Landscape { 
} 
- (BOOL)shouldAutorotate 
{ 
    return YES; 
} 
- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskAllButUpsideDown; 
} 

3)控制器,以你可以從控制器返回,可在橫向旋轉 - 這是很重要的,否則他們remaind景觀從景觀風險投資回報。

- (BOOL)shouldAutorotate 
{ 
    return YES; 
} 
- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

4)(也許不是必要的,但可以肯定的..) - 您使用,並添加子類導航控制器(S):

- (BOOL)shouldAutorotate 
{ 
    return YES; 
} 
- (NSUInteger)supportedInterfaceOrientations 
{ 
    UIViewController* vc = [self visibleViewController]; 
    if (vc) { 
     if ([vc respondsToSelector:@selector(needIos6Landscape)]) { 
      return [vc supportedInterfaceOrientations]; 
     } 
    } 
    return UIInterfaceOrientationMaskPortrait; 
} 

的重要步驟,是要求只定位控制器從你的應用程序,因爲在控制器之間的轉換,一段時間有一些系統控制器作爲根,並將返回不正確的值(這花了我2小時找出,這是它不工作的原因)。