当iPad从纵向模式进入横向模式时,如何更改UINavigationBar背景图像?

时间:2013-08-01 10:28:14

标签: objective-c uinavigationcontroller uinavigationbar

我正在创建一个应用程序,其中我在UINavigationBar背景中使用徽标。当iPad从纵向模式进入横向模式时,该徽标会变得拉伸并且看起来很难看。我为纵向和横向模式制作了两个不同的背景图像,然后使用以下代码更改它:

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

}

但那不起作用。它没有改变任何东西。知道我该怎么办?提前谢谢。

1 个答案:

答案 0 :(得分: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;
    }