0

我正在制作一个应用程序,其中我在 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];
}

}

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

4

1 回答 1

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;
    }
于 2013-08-01T10:42:07.237 回答