UITabBar selectionIndicatorImage填充 [英] UITabBar selectionIndicatorImage padding

查看:105
本文介绍了UITabBar selectionIndicatorImage填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的 UITabBar 设置了一个自定义指标图像

I set a custom indicator image for my UITabBar like this

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

并在 tabBarSelectedImage周围获得4px填充 。是否可以将填充设置为0px?这样我的 tabBarSelectedImage 填充整个空格并且没有边框可见?

and get a 4px padding around my tabBarSelectedImage. Is it possible to set that padding to 0px? So that my tabBarSelectedImage fills the entire space and no border is visible?

推荐答案

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

Here is the solution of your problem... I was not actually doing this...I was doing something else but the following code will help you a lot....First I tell you what I did...


  1. 我制作了 UITabbar 的类别,并在其中实现了以下方法

  1. I made a catagory of UITabbar and implement the following method in that

- (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 并做了在该方法中跟随。

Now I called the above method in my custom class of UITabbarController... I override the method
-(void)setSelectedIndex:(NSUInteger)selectedIndex and did the following in that method.

  -(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.如果有任何问题你可以告诉我。
干杯

You can optimize the code to avoid making catagory or subclassing...but for that you must have to grip on Objective C. In case of any issue you can tell me. Cheers

这篇关于UITabBar selectionIndicatorImage填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆