2011-04-27 8 views
7

मुझे चार टैब मिल गए हैं। मैं टैब आइकन रंग को डिफ़ॉल्ट नीले से लाल (या शायद किसी भी रंग) में बदलने में सक्षम था और यह पूरी तरह से ठीक काम करता है। समस्या यह है कि यह केवल तीन tabbaritems के लिए काम करता है और अंतिम एक डिफ़ॉल्ट नीला है। नीचे कोड है। मैं इसे rootviewcontrollerAppDelegate.m में कोडिंग कर रहा हूं आप अपने एपडेगेट में नीचे दिए गए कोड को चिपकाकर इसे आजमा सकते हैं। क्या आप लोग मेरी मदद कर सकते हैं मैं बहुत अच्छा होगा!डिफ़ॉल्ट नीले रंग से टैबबार आइकन रंग कैसे बदलें?

@implementation UITabBar (ColorExtensions) 

- (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; 

contextRect.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]; 

} 

} 
@end 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions 
{  

    [[tabBarController tabBar] recolorItemsWithColor:[UIColor redColor] shadowColor:[UIColor blackColor] shadowOffset:CGSizeMake(0.0f, -1.0f) shadowBlur:3.0f]; 

    [self.window addSubview:tabBarController.view]; 

     [self.window makeKeyAndVisible]; 

     [self addTabBarArrow]; 

     return YES; 
} 

enter image description here

उत्तर

2

आत्म जोड़ने tabbar-मद के लिए कोई समस्या नहीं है, मैं 4 आइटम के लिए इस कोड का परीक्षण;

लेकिन अपने पिछले tabbar आइटम एक प्रणाली tabbar आइटम ("...." "अधिक" आइटम) है, इसलिए इस कोड हो सकता है इसके लिए किसी काम का नहीं है; यह सिर्फ आपके सेट में छवि का उपयोग नहीं करता है;

+0

धन्यवाद आदमी ... मैं कोड ऊपर एक और problem..this बस केवल एक application..i के लिए काम किया में फंस गया है अन्य आवेदन में जिले में एक ही कोड की कोशिश की यह दीन काम ... CUD यू stuff.probably इस कोशिश .. कुछ अन्य अनुप्रयोगों में ... और मुझे जवाब दें – kingston

3

आपके साझाकरण के लिए धन्यवाद।

लेकिन वहाँ कुछ खामियां जहां iPhone4 या iPod4 पर तैनाती जो रेटिना प्रदर्शन कर रहे हैं। टैरबार में चयनित आइकन अचयनित से छोटा होगा।

तो मैं यहाँ मेरी ठीक साझा करना चाहते हैं:

CGSize orginalSize = [[item selectedImage] size]; 
double scaleFactor = 1; 
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
    scaleFactor = [[UIScreen mainScreen] scale]; 
} 
    contextRect.size = CGSizeMake(orginalSize.width*scaleFactor, orginalSize.height*scaleFactor); 

// Retrieve source image and begin image context 
UIImage *itemImage = [item image]; 
double imageScale = 1; 
if ([itemImage respondsToSelector:@selector(scale)]) { 
    imageScale = itemImage.scale; 
} 
CGSize itemImageSize = CGSizeMake(itemImage.size.width*imageScale, itemImage.size.height*imageScale); 

अगर मैं गलत हूँ, नि: शुल्क शुल्क कृपया मुझे पता है :)

0
@implementation MoreViewController 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) 
    { 
     self.title = @"More"; 
     self.tabBarItem.image=[UIImage imageNamed:@"more.png"]; // here more.png is Yellow Image 
    } 
    return self; 
} 

//....... 
@end 
8
[[UITabBar appearance] setSelectedImageTintColor:[UIColor redColor]]; 
0

अपने परिसंपत्ति फ़ोल्डर में जाएँ , संपत्ति खोजने के लिए और पहचान इंस्पेक्टर पर क्लिक करें, और मूल छवि को बदलने "प्रस्तुत के रूप में"

enter image description here