2012-04-05 47 views
20

我設置自定義指標圖像我UITabBar這樣UITabBar selectionIndicatorImage填充

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

,並讓我tabBarSelectedImage圍繞4PX填充。是否有可能將該填充設置爲0px?這樣我的tabBarSelectedImage就可以填滿整個空間了,沒有邊框可見了嗎?

回答

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.握在任何問題,你能告訴我的情況下。 乾杯

-2
  1. 首先,setSelectionIndicatorImage[UIColor clearColor]
  2. 其次,在您添加任何viewcontrollers到tabbarcontroller,添加圖像作爲一個子視圖的TabBar。然後它將出現在tabbaritems下作爲指示圖像
  3. 實施tabbarcontroller delegate方法來更改圖像的位置。

我已經實現了這種方式,它的工作原理非常完美。如果你想調整tabbar不同的方向,你應該調整一下。您可以繼承UITabbarController並實施layoutSubview方法。

4

我找到了一種簡單的解決方案:

self.tabBar.frame = CGRectInset(self.tabBar.frame,0,-6); 
+0

太棒了!每邊2px對iOS 7來說已經足夠了,否則項目圖標會移向邊緣。 – hybridcattt 2014-02-05 07:57:48

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 
+0

這非常有用! – orkenstein 2015-08-25 12:12:44