2012-04-05 11 views
20

मैं इसUITabBar selectionIndicatorImage गद्दी

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

की तरह मेरे UITabBar के लिए एक कस्टम सूचक छवि सेट और मेरे tabBarSelectedImage के चारों ओर एक 4px गद्दी मिलता है। क्या 0 पैक्स पर उस पैडिंग को सेट करना संभव है? तो मेरा 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]; 
    
        } 
        } 
        } 
    
        } 
    

आप कोड वर्ग या उपवर्गीकरण बनाने से बचने के अनुकूलन कर सकते हैं ... लेकिन उसके लिए आपको उद्देश्य सी पर पकड़ करने के लिए आवश्यक किसी भी मुद्दे आप मुझे बता सकते के मामले में। चीयर्स

-2
  1. पहले, setSelectionIndicatorImage[UIColor clearColor]-
  2. दूसरा, इससे पहले कि आप tabbarcontroller के लिए किसी भी viewcontrollers जोड़ने के लिए, tabbar करने के लिए एक subview के रूप अपना चित्र जोड़ें। फिर यह टैबबारिटम्स के तहत दिखाई देगा क्योंकि सूचक छवि
  3. छवि की स्थिति बदलने के लिए tabbarcontroller delegate विधि लागू करें।

मैंने इस तरह से लागू किया है और यह सही काम करता है। यदि आप विभिन्न उन्मुखताओं के लिए tabbar का आकार बदलना चाहते हैं तो आपको थोड़ा सा बदलाव करना चाहिए। आप UITabbarController उपखंड कर सकते हैं और layoutSubview विधि लागू कर सकते हैं।

4

मैं एक तरह से सरल समाधान पाया:

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

बढ़िया! प्रत्येक पक्ष पर 2px आईओएस 7 के लिए पर्याप्त है, अन्यथा आइटम आइकन किनारे पर स्थानांतरित दिखते हैं। – hybridcattt

1

ठीक है, यहाँ है कि मैं क्या पाया है गद्दी केवल प्रतीत होता है कि जब पुनः आकार देने योग्य छवियों का उपयोग कर रहा है। गैर आकार बदलने योग्य छवियों का उपयोग करते समय, पैडिंग नहीं है।

इसलिए, संभावित समाधान UITabBar को उपclass करना है और आइटम आकार में जब भी 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

संबंधित मुद्दे