我为我的UITabBar设置了一个自定义的指示器图像,如下所示
UIImage *tabBarSelectedImage = [[UIImage imageNamed:@"tabBar_selected"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UITabBar appearance] setSelectionIndicatorImage:tabBarSelectedImage];在我的tabBarSelectedImage周围增加4px的填充。是否可以将填充设置为0px?这样我的tabBarSelectedImage就会填满整个空间,而且看不到边框了?
发布于 2012-11-13 20:43:05
这是你问题的解决方案..。我不是真的在做这个……我在做别的事情,但是下面的代码会帮助你lot....First,我告诉你我做了什么……
UITabbar进行了分类,并在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。如果有任何问题,你可以告诉我。干杯
发布于 2013-05-30 17:47:06
我找到了一种更简单的解决方案:
self.tabBar.frame = CGRectInset(self.tabBar.frame,0,-6);发布于 2015-02-13 18:04:13
好吧,这里我发现只有当使用可调整大小的图像时,填充才会出现。当使用不可调整大小的图像时,填充不在那里。
因此,一种可能的解决方案是创建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];
}
}
@endhttps://stackoverflow.com/questions/10035302
复制相似问题