2012-09-24 76 views
4

我有一張桌子上有一大堆來自plist文件的東西,然後點擊其中每個人都會將您帶到一個新視圖xib。iOS 6風景和人像取向

我有一個的.xib內2次,一個縱向和一個景觀

在我的.h文件我有這樣的:

IBOutlet UIView *portraitView; 
IBOutlet UIView *landscapeView; 

@property (nonatomic, retain) IBOutlet UIView *portraitView; 
@property (nonatomic, retain) IBOutlet UIView *landscapeView; 

在我的M檔這樣的:

[super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 

} 

- (void) orientationChanged:(id)object 
{ 
    UIInterfaceOrientation interfaceOrientation = [[object object] orientation]; 

if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     self.view = self.portraitView; 
    } 
    else 
    { 
     self.view = self.landscapeView; 
    } 
} 

- (void)viewDidUnload 
{ 

    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) { 

     self.view = portraitView; 
    } else if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) { 

     self.view = landscapeView; 
    } 
    return YES; 

} 

@end 

一切都在iOS 5完美工作,在需要時顯示風景或肖像。

現在隨着iOS 6的更新,一切都是一團糟。

如果我在表格(縱向)視圖中單擊一個項目,它將顯示正確的縱向,如果我旋轉到橫向,視圖也顯示正確的視圖,但是在橫向上,如果我回到表格並選擇另一個項目,它將顯示縱向視圖而非橫向。

如果我這樣做,但開始景觀,它會顯示肖像。

所以,現在的方向不適用於任何事情。

我的其他視圖也使用故事板。他們是肖像畫,總是像那樣,現在他們旋轉,縮小一切,並把我的應用程序視爲垃圾。

1-我該如何修復.xib方向的東西?
2-如何修復故事板的方向? (他們是靜態的,現在一切都在旋轉(根本沒有代碼))

謝謝。

回答

13

我認爲我有一個解決辦法。這是醜陋的,但它的工作... 與iOS6蘋果建議現在使用2差異XIB文件之間切換肖像&風景視圖。

但是,如果您想通過2個UIView IBOutlet之間的「切換」來使用iOS 5.0中允許的前一個方法,那麼您可以嘗試我的難看的工作解決方案。這個想法是根據方向旋轉視圖。

1)在您的viewDidLoad,訂閱取向通知:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 
} 

2)添加一個由通知調用的方法:

-(void)orientationChanged:(NSNotification *)object{ 
    NSLog(@"orientation change"); 
    UIDeviceOrientation deviceOrientation = [[object object] orientation]; 
    if(deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation == UIInterfaceOrientationPortraitUpsideDown){ 
     self.view = self.potraitView; 
     if(deviceOrientation ==UIInterfaceOrientationPortraitUpsideDown){ 
      NSLog(@"Changed Orientation To PortraitUpsideDown"); 
      [self portraitUpsideDownOrientation]; 
     }else{ 
      NSLog(@"Changed Orientation To Portrait"); 
      [self portraitOrientation]; 
     } 
    }else{ 
     self.view = self.landscapeView; 
     if(deviceOrientation ==UIInterfaceOrientationLandscapeLeft){ 
      NSLog(@"Changed Orientation To Landscape left"); 
      [self landscapeLeftOrientation]; 
     }else{ 
      NSLog(@"Changed Orientation To Landscape right"); 
      [self landscapeRightOrientation]; 
     } 

    } 
} 

3)最後,加旋轉方法對於每個方向:

-(void)landscapeLeftOrientation{ 

    // Rotates the view. 
    CGAffineTransform transform = CGAffineTransformMakeRotation(-(3.14159/2)); 
    self.view.transform = transform; 
    // Repositions and resizes the view. 
    CGRect contentRect = CGRectMake(0, 0, 480, 320); 
    self.view.bounds = contentRect; 
} 
-(void)landscapeRightOrientation{ 

    // Rotates the view. 
    CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159/2); 
    self.view.transform = transform; 
    // Repositions and resizes the view. 
    CGRect contentRect = CGRectMake(0, 0, 480, 320); 
    self.view.bounds = contentRect; 
} 
-(void)portraitOrientation{ 

    // Rotates the view. 
    CGAffineTransform transform = CGAffineTransformMakeRotation(0); 
    self.view.transform = transform; 
    // Repositions and resizes the view. 
    CGRect contentRect = CGRectMake(0, 0, 320, 480); 
    self.view.bounds = contentRect; 
} 
-(void)portraitUpsideDownOrientation{ 

    // Rotates the view. 
    CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159); 
    self.view.transform = transform; 
    // Repositions and resizes the view. 
    CGRect contentRect = CGRectMake(0, 0, 320, 480); 
    self.view.bounds = contentRect; 
} 

我建議你做一個自定義的UIViewController類,並繼承e從這個類節省冗餘代碼。 如果你想同時支持ios5和ios6的解決方案,你可以使用#endif宏在你的控制器中包含這兩個代碼。

乾杯

1

無需發送和接收通知:

在你appdelegate.m以下方法

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 

總是被調用,檢查窗口的方向, 這樣一個簡單的方法周圍是你的appdelegate.m

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ 

    NSUInteger orientations = UIInterfaceOrientationMaskPortrait; 

    if(self.window.rootViewController){ 
     UIViewController *presentedViewController ; 
     if ([self.window.rootViewController isKindOfClass:([UINavigationController class])]) 
     { 
      presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject]; 
     } 
     else if ([self.window.rootViewController isKindOfClass:[UITabBarController class]]){ 
      UITabBarController *controller = (UITabBarController*)self.window.rootViewController; 

      id selectedController = [controller presentedViewController]; 

      if (!selectedController) { 
       selectedController = [controller selectedViewController]; 
      } 

       if ([selectedController isKindOfClass:([UINavigationController class])]) 
       { 
        presentedViewController = [[(UINavigationController *)selectedController viewControllers] lastObject]; 
       } 
       else{ 
        presentedViewController = selectedController; 
       } 
     } 
     else 
     { 
      presentedViewController = self.window.rootViewController; 
     } 

     if ([presentedViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) { 
      orientations = [presentedViewController supportedInterfaceOrientations]; 
     } 
    } 

    return orientations; 
} 

和實施

- (NSUInteger)supportedInterfaceOrientations 
在各自的視圖控制器

- (NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskPortrait; //Or anyother orientation of your choice 
} 

,並執行對方向改變突發動作

,實現以下方法

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

這是非常晚的答案,我仍然認爲我應該與你分享這件事,以防萬一,

我有同樣的問題。

shouldAutorotateToInterfaceOrientation已棄用iOS 6以上版本。

您需要該並聯方法與新supportedInterfaceOrientationsshouldAutorotate方法。

而且這是非常非常重要的,你需要確保你的應用程序委託的的applicationDidFinishLaunching方法,而不是簡單地把視圖控制器的視圖設置根控制器(或導航控制器或的TabBar控制器根據在你正在使用的)作爲窗口的子視圖。