2013-08-01 69 views
0

我正在製作一個應用程序,其中我在UINavigationBar背景中使用了一個徽標。當iPad從縱向模式轉換到橫向模式時,標誌會變得難看,看起來很醜。我做了兩個不同的背景圖片無論是縱向和橫向模式,然後就用下面的代碼進行更改:當iPad從縱向模式轉換到橫向模式時,如何更改UINavigationBar背景圖像?

-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ 
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait){ 
      NSLog(@"orientation is UIInterfaceOrientationPortrait"); 

      UINavigationBar *navBar = [[self navigationController] navigationBar]; 
      [navBar setBackgroundImage:[UIImage imageNamed:@"portrait_bg.png"] forBarMetrics:UIBarMetricsDefault]; 

    }else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){ 
      NSLog(@"orientation is UIInterfaceOrientationLandscapeLeft"); 
      UINavigationBar *navBar = [[self navigationController] navigationBar]; 
      [navBar setBackgroundImage:[UIImage imageNamed:@"landscape_bg.png"] forBarMetrics:UIBarMetricsDefault]; 
    }else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){ 
      NSLog(@"orientation is UIInterfaceOrientationLandscapeRight"); 
      UINavigationBar *navBar = [[self navigationController] navigationBar]; 
      [navBar setBackgroundImage:[UIImage imageNamed:@"landscape_bg.png"] forBarMetrics:UIBarMetricsDefault]; 

    }else{ 
      NSLog(@"orientation is UIInterfaceOrientationPortrait 2"); 
      UINavigationBar *navBar = [[self navigationController] navigationBar]; 
      [navBar setBackgroundImage:[UIImage imageNamed:@"portrait_bg.png"] forBarMetrics:UIBarMetricsDefault]; 
} 

}

但那不是工作。它沒有改變任何東西。任何想法我怎麼能做到這一點?提前致謝。

回答

0

您必須使用:

 - (void)viewDidLoad { 
     [super viewDidLoad]; 

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

     } 

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

     if(orientation == UIInterfaceOrientationPortrait) 
     { 
      NSLog(@"orientation is UIInterfaceOrientationPortrait"); 

      // Assuming "self" is a view controller pushed on to a UINavigationController stack 
      [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"portrait_bg.png"] forBarMetrics:UIBarMetricsDefault]; 
     } 
     else if(orientation == UIInterfaceOrientationLandscapeLeft) 
     { 
      NSLog(@"orientation is UIInterfaceOrientationLandscapeLeft"); 

      [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"landscape_bg.png"] forBarMetrics:UIBarMetricsDefault]; 
     } 
     else if(orientation == UIInterfaceOrientationLandscapeRight) 
     { 
      NSLog(@"orientation is UIInterfaceOrientationLandscapeRight"); 

      [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"landscape_bg.png"] forBarMetrics:UIBarMetricsDefault]; 
     } 
     else 
     { 
      NSLog(@"orientation is UIInterfaceOrientationPortrait 2"); 

      [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"portrait_bg.png"] forBarMetrics:UIBarMetricsDefault]; 
     } 
    } 

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    { 
     return YES; 
    } 
+0

沒有幫助。並有一個警告從枚舉類型「UIDeviceOrientation」(又名'枚舉UIDeviceOrientation')隱式轉換爲不同的枚舉類型'UIInterfaceOrientation'(又名'枚舉UIInterfaceOrientation') – Piscean

+0

其顯示日誌但不會更改UINavigationBar背景圖像 – Piscean

相關問題