0

I know it's possible to customize UITabBarItem in iOS 5 using

[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_sel"]];

The tabbar_sel image has the width 120px (640px/5). For landscape mode I need to change this to a image with 190x width.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    NSLog(@"landscape.");
    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_sel_l"]];
    } else {
    NSLog(@"normal.");
    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_sel"]];
    }
}

However, this does not work, Either in delegate class or in the ViewController. I also already tried this but it leads to a crash.

[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_sel"] 
forBarMetrics:UIBarMetricsDefault];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_sel_l"] forBarMetrics:UIBarMetricsLandscapePhone];

1 Answers1

0

willRotateToInterfaceOrientation: will be called when the view is loading to check to wich orientations it should auto rotate, it does not however ask this every time it tries to rotate. You can observe when the orientation changes like so:

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

- (void) orientationChange: (NSNotification *) notification {
  UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
  if (orientation == UIDeviceOrientationPortrait) {
     NSLog(@"portrait");
  }else if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){
     NSLog(@"landscape");
  }
}
Pieter Gunst
  • 139
  • 5
  • I checked it and edited my code, this should work to indicate changes in going from portrait to landscape and vice versa. Hower I'm not claiming that combined with your provided code it will give the required effect. I'm not sure that by using setSelectionIndicatorImage: it will automaticly repaint aswell. – Pieter Gunst Jul 31 '12 at 13:41