首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITabBar selectionIndicatorImage填充

UITabBar selectionIndicatorImage填充
EN

Stack Overflow用户
提问于 2012-04-06 04:11:10
回答 3查看 7.1K关注 0票数 20

我为我的UITabBar设置了一个自定义的指示器图像,如下所示

代码语言:javascript
复制
UIImage *tabBarSelectedImage = [[UIImage imageNamed:@"tabBar_selected"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UITabBar appearance] setSelectionIndicatorImage:tabBarSelectedImage];

在我的tabBarSelectedImage周围增加4px的填充。是否可以将填充设置为0px?这样我的tabBarSelectedImage就会填满整个空间,而且看不到边框了?

EN

回答 3

Stack Overflow用户

发布于 2012-11-13 20:43:05

这是你问题的解决方案..。我不是真的在做这个……我在做别的事情,但是下面的代码会帮助你lot....First,我告诉你我做了什么……

  1. 我对UITabbar进行了分类,并在
    1. 中实现了以下方法*)UITabbar shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur { CGColorRef cgColor = color CGColor;CGColorRef cgShadowColor = shadowColor;for ( *item in self items) { if (item respondsToSelector:@selector() && item & item respondsToSelector:@selector()){;你可以给出我们想要的contextRect的大小,而不是下面一行。//只需更改方法参数,并在其中包含所需大小的参数。//而这个所需的尺寸将是标签栏按钮size...so你将传递的尺寸//你的标签栏按钮here...because在图片的背面有一个标签栏按钮,如果//设置了图片的按钮大小,它将占据整个按钮的区域。contextRect.size =期望大小// [项目selectedImage大小];//检索源图片并开始图片上下文UIImage *itemImage =项目图片;CGSize itemImageSize = itemImage大小;CGPoint itemImagePosition;itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) / 2);itemImagePosition.y=ceilf((itemImageSize.width-itemImageSize.height)/2);itemImage CGContextRef c= UIGraphicsGetCurrentContext();//设置阴影CGContextSetShadowWithColor(c,shadowOffset,shadowBlur,cgShadowColor);//设置透明层和剪辑,屏蔽CGContextBeginTransparencyLayer(c,NULL);CGContextScaleCTM(c,1.0,-1.0);CGContextClipToMask(c,CGRectMake(itemImagePosition.x,-itemImagePosition.y,itemImageSize.width,-itemImageSize.height),itemImage CGImage);//填充结束透明层CGContextSetFillColorWithColor(c,cgColor);contextRect.size.height = -contextRect.size.height;CGContextFillRect(c,contextRect);CGContextEndTransparencyLayer(c);//设置所选图像和结束上下文项CGContextEndTransparencyLayer UIGraphicsEndImageContext();//更新视图项_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 *i=1= self.tabBar.subviews objectAtIndex:i;NSLog(@"class:%@",NSStringFromClass(view class));if ([NSStringFromClass(视图类) 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 shadowBlur:0.5];}

你可以优化代码,以避免制作分类或subclassing...but,因为你必须抓住目标C。如果有任何问题,你可以告诉我。干杯

票数 5
EN

Stack Overflow用户

发布于 2013-05-30 17:47:06

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

代码语言:javascript
复制
self.tabBar.frame = CGRectInset(self.tabBar.frame,0,-6);
票数 5
EN

Stack Overflow用户

发布于 2015-02-13 18:04:13

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

因此,一种可能的解决方案是创建UITabBar的子类,并在项目大小发生变化时配置selectionIndicatorImage

代码语言:javascript
复制
@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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10035302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档