Question

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];
Was it helpful?

Solution

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");
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top