0

我知道可以使用在 iOS 5 中自定义 UITabBarItem

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

tabbar_sel 图像的宽度为 120px (640px/5)。对于横向模式,我需要将其更改为 190x 宽度的图像。

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

但是,这不起作用,无论是在委托类中还是在 ViewController 中。我也已经尝试过了,但这会导致崩溃。

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

1 回答 1

0

willRotateToInterfaceOrientation: 将在视图加载时调用,以检查它应该自动旋转的方向,但它不会在每次尝试旋转时询问这个。您可以观察方向何时改变,如下所示:

[[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");
  }
}
于 2012-07-31T10:19:50.367 回答