2012-07-31 72 views
0

我知道这是可能使用定制UITabBarItem

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

的tabbar_sel图像具有宽度120像素(640像素/ 5)来定制UITabBarItem在IOS 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]; 

回答

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"); 
    } 
} 
+0

感谢您的回答,但这不起作用。 – 2012-07-31 13:33:39

+0

我检查了它并编辑了我的代码,这应该能够指示从纵向到横向的变化,反之亦然。 Hower我并没有声称与您提供的代码相结合,它会提供所需的效果。我不确定通过使用setSelectionIndicatorImage:它会自动重新绘制。 – 2012-07-31 13:41:28