20

UITabBar为此设置了自定义指标图像

UIImage *tabBarSelectedImage = [[UIImage imageNamed:@"tabBar_selected"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UITabBar appearance] setSelectionIndicatorImage:tabBarSelectedImage];

并在我的tabBarSelectedImage. 是否可以将该填充设置为0px?这样我的tabBarSelectedImage填充了整个空间并且没有边框可见?

4

4 回答 4

5

我找到了一种更简单的解决方案:

self.tabBar.frame = CGRectInset(self.tabBar.frame,0,-6);
于 2013-05-30T09:47:06.477 回答
5

这是您问题的解决方案...我实际上并没有这样做...我正在做其他事情,但是以下代码会对您有很大帮助...首先我告诉您我做了什么...

  1. 我做了一个分类UITabbar并在其中实现了以下方法

    - (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor 
    shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur
    {
    CGColorRef cgColor = [color CGColor];
    CGColorRef cgShadowColor = [shadowColor CGColor];
    for (UITabBarItem *item in [self items]) {
    
    if ([item respondsToSelector:@selector(selectedImage)] &&
        [item respondsToSelector:@selector(setSelectedImage:)] &&
        [item respondsToSelector:@selector(_updateView)])
    {
        CGRect contextRect;
        contextRect.origin.x = 0.0f;
        contextRect.origin.y = 0.0f;
    
        //instead of following line you can give our own desired size of contextRect. 
        //just change the method parameters and include a parameter of desired size in it.
        // and this desired size would be the tabbarbutton size...so you will pass the size of 
        // you tabbarbutton here...because on the back of image there is a tabbarbutton and if
        // set the image of button size it will occupy whole the are of button.
    
        contextRect.size = desired size //[[item selectedImage] size];
        // Retrieve source image and begin image context
        UIImage *itemImage = [item image];
        CGSize itemImageSize = [itemImage size];
    
        CGPoint itemImagePosition; 
        itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);
        itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) / 2);
        UIGraphicsBeginImageContext(contextRect.size);
        CGContextRef c = UIGraphicsGetCurrentContext();
    
        // Setup shadow
        CGContextSetShadowWithColor(c, shadowOffset, shadowBlur, cgShadowColor);
    
        // Setup transparency layer and clip to mask
        CGContextBeginTransparencyLayer(c, NULL);
        CGContextScaleCTM(c, 1.0, -1.0);
        CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y,
        itemImageSize.width, -itemImageSize.height), [itemImage CGImage]);
    
        // Fill and end the transparency layer
        CGContextSetFillColorWithColor(c, cgColor);
        contextRect.size.height = -contextRect.size.height;
        CGContextFillRect(c, contextRect);
        CGContextEndTransparencyLayer(c);
    
    
        // Set selected image and end context
        [item setSelectedImage:UIGraphicsGetImageFromCurrentImageContext()];
        UIGraphicsEndImageContext();
        // Update the view
        [item _updateView];
       }
       }
    
       }
    

    现在我在我的自定义类 UITabbarController 中调用了上述方法...我重写了该方法
    -(void)setSelectedIndex:(NSUInteger)selectedIndex并在该方法中执行了以下操作。

      -(void)setSelectedIndex:(NSUInteger)selectedIndex {
           self.selectedViewController = [self.viewControllers objectAtIndex:selectedIndex];
           NSLog(@"selectedIndex:%d, totalCount:%d",selectedIndex,[self.tabBar.subviews count]);
       for (uint i=1; i < [self.tabBar.subviews count]; i++)
       {
          UIView *view = [self.tabBar.subviews objectAtIndex:i];
          NSLog(@"class:%@",NSStringFromClass([view class]));
       if ([NSStringFromClass([view class]) isEqualToString:@"UITabBarButton"])
       {
        //view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y,  
        view.frame.size.width, self.tabBar.frame.size.height);
          NSLog(@"selectedIndex:%d,i:%d",self.selectedIndex,i);
        if (self.selectedIndex+1==i) {
            [self.tabBar recolorItemsWithColor:[UIColor whiteColor] shadowColor:[UIColor 
                                blackColor] shadowOffset:view.frame.size shadowBlur:0.5];
    
        }
        } 
        }
    
        }
    

您可以优化代码以避免进行分类或子类化......但为此您必须掌握目标 C。如果有任何问题,您可以告诉我。干杯

于 2012-11-13T12:43:05.293 回答
1

好吧,我发现只有在使用可调整大小的图像时才会出现填充。使用不可调整大小的图像时,填充不存在。

因此,一个可能的解决方案是子类化 UITabBar 并selectionIndicatorImage在项目大小更改时配置。

@interface TKTabBar

@end

@implementation TKTabBar
{
    CGSize _selectionIndicatorImageSize;
}

- (void)tk_refreshSelectionIndicatorImageForItemSize:(CGSize)itemSize
{
    // Recompute the selection indicator image only if the size of the item has changed.
    if (!CGSizeEqualToSize(itemSize, _selectionIndicatorImageSize))
    {
        _selectionIndicatorImageSize = itemSize;

        // Compute here the new image from the item size.
        // In this example I'm using a Cocoa Pod called UIImage+Additions to generate images dynamically.

        UIImage *redImage = [UIImage add_imageWithColor:[UIColor add_colorWithRed255:208 green255:75 blue255:43] size:CGSizeMake(itemSize.width, 2)];
        UIImage *clearImage = [UIImage add_imageWithColor:[UIColor clearColor] size:CGSizeMake(itemSize.width, itemSize.height)];
        UIImage *mixImage = [clearImage add_imageAddingImage:redImage offset:CGPointMake(0, itemSize.height-2)];

        // Finally, I'm setting the image as the selection indicator image.
        [self setSelectionIndicatorImage:mixImage];
    }
}

// Using the layout subviews method to detect changes on the tab size
- (void)layoutSubviews
{
    [super layoutSubviews];

    // Only needed if at least one item
    if (self.items.count > 0)
    {
        CGSize itemSize = CGSizeZero;

        // Iterating over all subviews
        for (UIView *view1 in self.subviews)
        {
            // Searching for "UITabBarButtons"
            if ([view1 isKindOfClass:NSClassFromString(@"UITabBarButton")])
            {
                itemSize = view1.bounds.size;
                break;
            }
        }

        // Applying the new item size
        [self tk_refreshSelectionIndicatorImageForItemSize:itemSize];
    }
}

@end
于 2015-02-13T10:04:12.537 回答
-3
  1. 首先,setSelectionIndicatorImage[UIColor clearColor]
  2. 其次,将任何视图控制器添加到之前tabbarcontroller,请将您的图像作为子视图添加到选项卡栏。然后它将作为指示图像出现在 tabbaritems 下
  3. 实现tabbarcontroller delegate改变图像位置的方法。

我已经实现了这种方式,并且效果很好。tabbar如果您想针对不同的方向调整大小,您应该稍微调整一下。您可以子类化UITabbarController并实现layoutSubview方法。

于 2012-11-14T01:26:39.560 回答