Image of tab item and selectedImage

I have a tab bar controller (this is a tab bar based application, so the tab bar is on MainWindow.xib). In this xib, I added 4 tab bar items and I set the image of the entire tab bar item. In this regard, I encountered two problems:

1) The image is white, but when I launch the application, it shows all the images in the tab element as gray. How can I make it look the same as in the original image.

2) I have a selected image that I want to add to the tab bar item that is currently selected. How can I do it?

UPDATED AFTER THE NICK BUTTON:

Hey, in iOS 5 you will need to write the following code in the application deletion to set the selected tab bar item and unselected image (the category solution will only work on 4):

if ([[[UIDevice currentDevice] systemVersion] floatValue]>4.9) { NSString *selectedImageName,*unselectedImageName; for (int counter = 0; counter < [self.tabBarController.tabBar.items count]; counter++) { if (counter==0) { selectedImageName = <someImagename>; unselectedImageName = <someImagename>; } else if (counter==1) { selectedImageName = <someImagename>; unselectedImageName = <someImagename>; } . . else { selectedImageName = <someImagename>; unselectedImageName = <someImagename>; } UIImage *selectedImage = [UIImage imageNamed:selectedImageName]; UIImage *unselectedImage = [UIImage imageNamed:unselectedImageName]; UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex:counter]; if ([item respondsToSelector:@selector(setFinishedSelectedImage:withFinishedUnselectedImage:)]) { [item setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage]; } } } 
+1
source share
2 answers

Add this category to your project. This will force the tab bar elements to use the original image as a disabled state instead of applying a gray gradient to them:

 @implementation UItabBarItem (CustomUnselectedImage) - (UIImage *)unselectedImage { return self.image; } @end 

It may seem like it is using private APIs, but I saw that it was used several times for applications that were approved. This is actually not a private method call, but simply an override.

If you need to specify different images for the selected and unselected images, it is best to use the property of the UITabBarItem tag and the switch statement, for example:

 @implementation UItabBarItem (Custom) - (UIImage *)selectedImage { switch (self.tag) { case 1: return [UIImage imageNamed:@"tab-selected1.png"]; case 2: return [UIImage imageNamed:@"tab-selected2.png"]; etc... } } - (UIImage *)unselectedImage { switch (self.tag) { case 1: return [UIImage imageNamed:@"tab-unselected1.png"]; case 2: return [UIImage imageNamed:@"tab-unselected2.png"]; etc... } } @end 

Then, in the interface builder, do not interfere with customizing the images of the tab bar items, as they will simply be ignored. Instead, set the tags according to the images specified in the switch statements.

Note that if an application has several tab stripes and you do not want all of them to be redefined in this way, you can define these methods in a subclass of UITabBarItem instead of a category. You can then set the class of tab bar items in the nib file as your own subclass instead of the usual UITabBarItems, and only those that will be affected will be affected.

EDIT:

Please note that with iOS 5 there is a better way to do this using the UIAppearance APIs. This method should still work, but who knows if Apple can start to fight it, there is now an officially supported approach. It’s better to use the new method if you don’t need iOS 4 support.

+6
source

Based on http://blog.theanalogguy.be/ works for me. Add category UItabBarItem (CustomUnselectedImage) - has no effect = (

* .h

 @interface CustomTabBarItem : UITabBarItem { UIImage *customHighlightedImage; UIImage *customNormalImage; } @property (nonatomic, retain) UIImage *customHighlightedImage; @property (nonatomic, retain) UIImage *customNormalImage; - (id)initWithTitle:(NSString *)title normalImage:(UIImage *)normalImage highlightedImage:(UIImage *)highlightedImage tag:(NSInteger)tag; @end 

and * .m

 #import "CustomTabBarItem.h" @implementation CustomTabBarItem @synthesize customHighlightedImage; @synthesize customNormalImage; - (id)initWithTitle:(NSString *)title normalImage:(UIImage *)normalImage highlightedImage:(UIImage *)highlightedImage tag:(NSInteger)tag{ [self initWithTitle:title image:nil tag:tag]; [self setCustomNormalImage:normalImage]; [self setCustomHighlightedImage:highlightedImage]; return self; } - (void) dealloc { [customHighlightedImage release]; customHighlightedImage=nil; [customNormalImage release]; customNormalImage=nil; [super dealloc]; } -(UIImage *) selectedImage { return self.customHighlightedImage; } -(UIImage *) unselectedImage { return self.customNormalImage; } @end 

happy coding =] -

+1
source

Source: https://habr.com/ru/post/907691/


All Articles