3

如何隐藏 ios 4.3 的 UITabBar 选择指示器而不是 5+

我需要在ios 4.3 中隐藏UITabBar 控制器的选择指示器。

4

2 回答 2

5

试试这个代码:

// iOS 5.0+
[self.tabBar setSelectionIndicatorImage:[[[UIImage alloc] init]autorelease]];


// for earlier versions

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    [self customizeTabBar];
}

- (void)customizeTabBar {
    NSString *imageName = [NSString stringWithFormat:@"tabBackground%i.png", tabBarCtrl.selectedIndex + 1];

    for(UIView *view in tabBarCtrl.tabBar.subviews) {  
         if([view isKindOfClass:[UIImageView class]]) {  
              [view removeFromSuperview];  
         }  
    }  
    UIImageView *background = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]] autorelease];
    [tabBarCtrl.tabBar insertSubview:background atIndex:0]; 
    [tabBarCtrl.tabBar bringSubviewToFront:background];
    //if needed, here must be adding UILabels with titles, I didn't need it.
} 

希望能帮助到你!:)

于 2012-06-05T23:52:07.253 回答
1

这是不可能的,但你可以试试这个技巧。

在控制器上(添加到 TabBarController 项的控制器) tabBarItem enable 设置为 false

[controller.tabBarItem setEnabled:NO];

当 TabBarController 出现时,您可以在 tabBarItem 上添加按钮,在这种情况下按钮添加到中心(TabbarItem 为 5)

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    UIImage *buttonImage = [UIImage imageNamed:@"1.png"]
    UIImage *highlightImage = [UIImage imageNamed:@"1_selected.png"]

    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
    button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(centerCliked:) forControlEvents:UIControlEventTouchUpInside];

    CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
    if (heightDifference < 0)
        button.center = self.tabBar.center;
    else
    {
        CGPoint center = self.tabBar.center;
        center.y = center.y - heightDifference/2.0 + 2;
        button.center = center;
    }
    [self.view addSubview:button];
}


- (IBAction)centerClicked:(id)sender {
     [self setSelectedIndex:2];
}
于 2012-06-06T19:59:57.863 回答