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

时间:2012-06-05 13:29:25

标签: iphone ios ipad

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

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

2 个答案:

答案 0 :(得分: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.
} 

希望它有所帮助! :)

答案 1 :(得分: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];
}