UITabBar not showing selected item images in ios 7 - objective-c

The icons show fine in ios 6 but not in ios 7. I'm setting the selected state in the viewController viewDidLoad method. When the user selects a tab bar item the image disappears.
Here is my code:
UITabBar *tabBar = self.tabBarController.tabBar;
if ([UITabBar instancesRespondToSelector:#selector(setSelectedImageTintColor:)]) {
[self.tabBarController.tabBar setSelectedImageTintColor:[UIColor whiteColor]];
}
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
[item0 setTitle:#"Home"];
[item1 setTitle:#"Calendar"];
[item2 setTitle:#"News"];
[item3 setTitle:#"My Events"];
[item0 setFinishedSelectedImage:[UIImage imageNamed:#"homeIconSelected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"home2.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:#"Calendar"] withFinishedUnselectedImage:[UIImage imageNamed:#"CalendarIconSelected"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:#"NewsIconSelected"] withFinishedUnselectedImage:[UIImage imageNamed:#"News"]];
[item3 setFinishedSelectedImage:[UIImage imageNamed:#"EventsIconSelected"] withFinishedUnselectedImage:[UIImage imageNamed:#"Events"]];
[item1 imageInsets];
[item2 imageInsets];
[item3 imageInsets];

You need to use tabBarItem initWithTitle:image:selectedImage
[[UITabBarItem alloc] initWithTitle:#"title" image:image selectedImage:imageSel];
in conjunction with changing the UIImage rendering mode:
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal
or (to apply parent views template tint mask, this option is default for Tab bar Items unless you opt out with the above rendering mode)
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate
here is a code sample for one tab bar item :-
UIImage *musicImage = [UIImage imageNamed:#"music.png"];
UIImage *musicImageSel = [UIImage imageNamed:#"musicSel.png"];
musicImage = [musicImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
musicImageSel = [musicImageSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.musicViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Music" image:musicImage selectedImage:musicImageSel];

Set bar item image's render mode to original can solve this problem.
This can be done by using images in the .xcassets, so you don't have to write lots of codes.
First step, drap&drop your bar item images to Assets.xcassets.
Second step, choose the bar item image, and change [Render As] to [Original Image]
ps: I usually set the TabBarController's tab bar items all by story board to avoid writing lots of code.

Add these lines of code in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
tabBarItem1.selectedImage = [[UIImage imageNamed:#"selectimg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.image = [[UIImage imageNamed:#"deselectimg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.title = #"xxxx";
tabBarItem2.selectedImage = [[UIImage imageNamed:#"selectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.image = [[UIImage imageNamed:#"deselectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.title = #"xxxx";
tabBarItem3.selectedImage = [[UIImage imageNamed:#"selectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.image = [[UIImage imageNamed:#"deselectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.title = #"xxxx";
tabBarItem4.selectedImage = [[UIImage imageNamed:#"selectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.image = [[UIImage imageNamed:#"deselectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.title = #"xxxx";
return YES;
}
this works for me.

No answers helped fixing this issue. The main reason is that my TabBarController wasn't my RootViewController.
The solution I used for Storyboards, and I just clicked my UITabButton and I added a runtime attribute for selectedImage:
For each of the different views associated with the UITabController.

After spending a couple of hours trying to make my custom tabbar to work for both iOS 6 & 7, that's what worked for me...
UITabBarController *tabBarController = (UITabBarController *)[[self window] rootViewController];
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
tabBarItem1.title = #"Home";
tabBarItem2.title = #"Map";
tabBarItem3.title = #"Weather";
tabBarItem4.title = #"Info";
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"cyexplore_home_white.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"cyexplore_home_black.png"]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:#"cyexplore_cloud_white.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"cyexplore_cloud_black.png"]];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:#"cyexplore_map_white.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"cyexplore_map_black.png"]];
[tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:#"cyexplore_info_white.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"cyexplore_info_black.png"]];
} else {
tabBarItem1.selectedImage = [[UIImage imageNamed:#"cyexplore_home_white"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.image = [[UIImage imageNamed:#"cyexplore_home_black"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.selectedImage = [[UIImage imageNamed:#"cyexplore_cloud_white"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.image = [[UIImage imageNamed:#"cyexplore_cloud_black"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.selectedImage = [[UIImage imageNamed:#"cyexplore_map_white"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.image = [[UIImage imageNamed:#"cyexplore_map_black"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.selectedImage = [[UIImage imageNamed:#"cyexplore_info_white"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.image = [[UIImage imageNamed:#"cyexplore_info_black"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
}
UIImage* tabBarBackground = [UIImage imageNamed:#"tabbar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"tabbar_selected.png"]];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor, nil] forState:UIControlStateSelected];

if you are working with storyboards you have to put the identifier : "custom" in Navigation Controller.
then :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Assign tab bar item with titles
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
(void)[tabBarItem1 initWithTitle:nil image:[UIImage imageNamed:#"home.png"] selectedImage:[UIImage imageNamed:#"home_selected.png"]];
(void)[tabBarItem2 initWithTitle:nil image:[UIImage imageNamed:#"home.png"] selectedImage:[UIImage imageNamed:#"home_selected.png"]];
(void)[tabBarItem3 initWithTitle:nil image:[UIImage imageNamed:#"home.png"] selectedImage:[UIImage imageNamed:#"home_selected.png"]];
// Change the tab bar background
UIImage* tabBarBackground = [UIImage imageNamed:#"tabbar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
return YES;
}
This works for me.

None of the answers worked for me - I am using MonoTouch but if you set the TintColor property of the UITabBar itself that will highlight the selected image with that color. In obj c it may be setTintColor function.

You should write to the function:
UIImage* tab_image = [UIImage imageNamed:#"tab_image.png"];
UIImage* tab_image_selected = [UIImage imageNamed:#"tab_image_selected.png"];
tab_image = [tab_image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tab_image_selected = [tab_image_selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem.image = tab_image;
self.tabBarItem.selectedImage = tab_image_selected;
I hope this helps

I had the same problem with Xcode 6.0.1 (6A317), seems to be a bug with Interface builder.
However, i've managed to solve it by leaving selected image empty in interface builder then at each viewDidLoad in my view controllers i did insert:
[self.navigationController.tabBarItem setSelectedImage:[UIImage imageNamed:#"imagename-selected"]];
it works well now, showing my selectedImage and with global tint mask.

Based on the answer from 132206, I made this method for AppDelegate, called from application:didFinishLaunchingWithOptions: with:
[self configureSelectedImages];
It obviously requires a strict naming convention for your tab images, but can also be re-used without editing. To state the obvious - name your selected tab bar images TabbarXXXSelected, where XXX equals the title of the tab bar item.
- (void)configureSelectedImages
{
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
for (UITabBarItem *tabBarItem in [tabBar items]) {
NSString *selectedImage = [NSString stringWithFormat:#"Tabbar%#Selected", [tabBarItem title]];
(void)[tabBarItem initWithTitle:[tabBarItem title] image:[tabBarItem image] selectedImage:[UIImage imageNamed:selectedImage]];
}
}

In your first view controller's .h file, I added the following:
#property (weak, nonatomic) IBOutlet UITabBarItem *mapViewTabBarItem;
#property (weak, nonatomic) IBOutlet UITabBarItem *profileViewTabBarItem;
#property (weak, nonatomic) IBOutlet UITabBarItem *notificationViewTabBarItem;
(note that the mapViewTabBarItem was linked by ctrl dragging the actual tab bar item into the list of property declarations at the top of the .h file)
Next, in the same view controller's .m file in the viewDidLoad, add the following:
self.tabBarItem = [self.tabBarController.tabBar.items objectAtIndex:0];
_mapViewTabBarItem.selectedImage = [[UIImage imageNamed:#"#2x-map-icon-selected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
self.tabBarItem.image = [[UIImage imageNamed:#"#2x-map-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
_profileViewTabBarItem = [self.tabBarController.tabBar.items objectAtIndex:1];
_profileViewTabBarItem.selectedImage = [[UIImage imageNamed:#"#2x-profile-icon-selected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
_profileViewTabBarItem.image = [[UIImage imageNamed:#"#2x-profile-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
_notificationViewTabBarItem = [self.tabBarController.tabBar.items objectAtIndex:2];
_notificationViewTabBarItem.selectedImage = [[UIImage imageNamed:#"#2x-notifications-icon-selected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
_notificationViewTabBarItem.image = [[UIImage imageNamed:#"#2x-notifications-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

I had a similar problem. I created the tab bar in storyboard and added all the images through the interface builder menus, none in code.
My fix was actually simple: under the attributes inspector window in IB, the Tab Bar Item field for "Selected Image" should be blank, and the Bar Item field for "Image" should be filled with the image you want.
I am running Xcode 6.0.1 and testing with iOS 8.0.2 devices.

Try this:
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:#"" image:[UIImage imageNamed:#"Icon-Small-50.png"] tag:100];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:#"" image:[UIImage imageNamed:#"image-50.png"] tag:200];
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:#"" image:[UIImage imageNamed:#"Icon-clip-50.png"] tag:300];
UITabBarItem *item4 = [[UITabBarItem alloc] initWithTitle:#"" image:[UIImage imageNamed:#"Icon-color-50.png"] tag:400];
UITabBarItem *item5 = [[UITabBarItem alloc] initWithTitle:#"" image:[UIImage imageNamed:#"Icon-lock-50.png"] tag:500];
[item1 setSelectedImage:[[UIImage imageNamed:#"Icon-Small-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item1 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[item2 setSelectedImage:[[UIImage imageNamed:#"image-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item2 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[item3 setSelectedImage:[[UIImage imageNamed:#"Icon-clip-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item3 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[item4 setSelectedImage:[[UIImage imageNamed:#"Icon-color-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item4 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[item5 setSelectedImage:[[UIImage imageNamed:#"Icon-lock-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item5 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
item1.image = [[UIImage imageNamed:#"Icon-Small-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item2.image = [[UIImage imageNamed:#"image-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item3.image = [[UIImage imageNamed:#"Icon-clip-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item4.image = [[UIImage imageNamed:#"Icon-color-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item5.image = [[UIImage imageNamed:#"Icon-lock-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

Use the code below to fix the image issue in iOS7:
[[UITabBarItem alloc] initWithTitle:#"title" image:[[UIImage imageNamed:#"YOUR_IMAGE.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:#"YOUR_SEL_IMAGE.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

I had the same issue. But working with StoryBoards prevented me from changing anything in the code. Leaving the image empty in the storyboard removed this obstacle for me. However putting the initWithTitle in the viewWillAppear method of the tab's viewcontroller gave me odd behaviour. First getting the selected image required an additional click and images did not show up for the non-initial tabs.
For me fixing this was adding the following code to the AppDelegate in the DidFinishLoadingWithOptions (similar to 132206 and Amitabha):
NSArray * vcs = [(UITabBarController*)self.window.rootViewController viewControllers];
UIViewController *tab0 = [[(UINavigationController*)[vcs objectAtIndex:0] viewControllers] objectAtIndex:0];
tab0.title = NSLocalizedString(#"Time", nil);
tab0.tabBarItem = [[UITabBarItem alloc] initWithTitle:tab0.title image:[UIImage imageNamed:#"Recents.png"] selectedImage:[UIImage imageNamed:#"RecentsSolid.png"]];
UIViewController *tab1 = [[(UINavigationController*)[vcs objectAtIndex:1] viewControllers] objectAtIndex:0];
tab1.title = NSLocalizedString(#"Expense", nil);
tab1.tabBarItem = [[UITabBarItem alloc] initWithTitle:tab1.title image:[UIImage imageNamed:#"Euro.png"] selectedImage:[UIImage imageNamed:#"EuroSolid.png"]];

It is easy and clean solution of category for UITabBar Items.
Just create category and use the Runtime Attribute and refer it from category like below.
#import "UITabBarItem+CustomTabBar.h"
#implementation UITabBarItem (CustomTabBar)
-(void)setValue:(id)value forKey:(NSString *)key {
if([key isEqualToString:#"tabtitle"]){
if([value isEqualToString:#"contacts"]) {
[self setSelectedImage:[[UIImage imageNamed:#"contacts-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
} else if([value isEqualToString:#"chat"]) {
[self setSelectedImage:[[UIImage imageNamed:#"chat-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
} else if([value isEqualToString:#"groupchat"]) {
[self setSelectedImage:[[UIImage imageNamed:#"groupchat-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
} else if([value isEqualToString:#"settings"]) {
[self setSelectedImage:[[UIImage imageNamed:#"settings-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
}
}
[self setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"Roboto-Regular" size:12.0f], NSFontAttributeName, [UIColor grayColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
}
#end

Old questions, but going to add my solution here as well for Xamarin.iOS/C# and those who want to set the images via Interface builder. I set the Selected Image and Image attributes via Interface Builder. Then in code I defined an InitTabs() method like this:
public void InitTabs(){
HomeTab.SelectedImage = HomeTab.SelectedImage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
HomeTab.Image = HomeTab.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
}
Call InitTabs() in ViewDidLoad and now the proper image will appear for both the selected and unselected state.

Here is a Swift solution for Swift-Guys :)
class CustomTabBar: UITabBar {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let btnNames = ["Title1", "Title2", "Title3", "Title4"]
for (item, name) in zip(items!, btnNames) {
item.image = UIImage(named: "bar\(name)Btn")?.imageWithRenderingMode(.AlwaysOriginal)
item.selectedImage = UIImage(named: "bar\(name)SelectedBtn")?.imageWithRenderingMode(.AlwaysOriginal)
item.title = name
item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor()], forState: .Normal)
item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState: .Selected)
}
}
}
What is exactly going on here:
Make array of btn titles and consider image file names to match them
Make For loop over tab bar items and just created btn titles array
Set barButtonItem's image and its selectedImage from the array
Set title text from the array
Set title text color for states .Normal and .Selected
Setting text colors part is important if you don't want to keep the item's title color gray for .Normal and blue for .Selected, as it is by default. This is often actual when you consider custom images for tab bar items.

Swift version of showing selected and unselected images and title with UIAppearance API
In your Appdelegate.m copy following code if you have tab base app.following code assume you have 4 tab bar.
let tabBarController: UITabBarController = (self.window!.rootViewController as! UITabBarController)
let tabBar:UITabBar = tabBarController.tabBar
let tabBarItem1:UITabBarItem = tabBar.items![0]
let tabBarItem2:UITabBarItem = tabBar.items![1]
let tabBarItem3:UITabBarItem = tabBar.items![2]
let tabBarItem4:UITabBarItem = tabBar.items![3]
tabBarItem1.title = "Home";
tabBarItem2.title = "Maps";
tabBarItem3.title = "My Plan";
tabBarItem4.title = "Settings";
tabBarItem1.selectedImage = UIImage(named: "home_selected.png")!
tabBarItem2.selectedImage = UIImage(named: "maps_selected.png")!
tabBarItem3.selectedImage = UIImage(named: "myplan_selected.png")!
tabBarItem4.selectedImage = UIImage(named: "settings_selected.png")!
tabBarItem1.image = UIImage(named: "home.png")!
tabBarItem2.image = UIImage(named: "maps.png")!
tabBarItem3.image = UIImage(named: "myplan.png")!
tabBarItem4.image = UIImage(named: "settings.png")!
let tabBarBackground: UIImage = UIImage(named: "tabbar.png")!
UITabBar.appearance().backgroundImage = tabBarBackground
UITabBar.appearance().selectionIndicatorImage = UIImage(named: "tabbar_selected.png")!
UITabBarItem.appearance().setTitleTextAttributes([
NSForegroundColorAttributeName : UIColor.whiteColor()
]
, forState: .Normal)
let titleHighlightedColor: UIColor = UIColor(red: 153 / 255.0, green: 192 / 255.0, blue: 48 / 255.0, alpha: 1.0)
UITabBarItem.appearance().setTitleTextAttributes([
NSForegroundColorAttributeName : titleHighlightedColor
]
, forState: .Highlighted)

After trying all of the other answers and struggling as they failed, I found the answer. The other answers, , don't seem to work in current swift version. In Swift 2.3, this works for me. For those who are still having trouble, try this:
tabBarItem.image = UIImage(named: "image_name")
searchVC.tabBarItem.selectedImage = UIImage(named:
"image_name_when_selected")?.imageWithRenderingMode(.AlwaysOriginal)

Related

How to add Image in MoreViewController of Tabbar Controller?

I have more than 6 ViewController in Tabbar controller so it will display 2 tab in moreViewcontroller Tableview
Now i dont know how to add image item and title for this 2 tab??
Below is code of My tabbar implemetation
NSArray *arrayVC=[[NSArray alloc]initWithObjects:objNewsFeedNavi,objReqNavi,objMsgNavi,objNotificationNavi,objProfileNavi,objChatNavi, nil];
[self.objTb setViewControllers:arrayVC];
UITabBarItem *itemNewsFeed=[self.objTb.tabBar.items objectAtIndex:0];
itemNewsFeed.title=#"News Feed";
itemNewsFeed.image = [[UIImage imageNamed:#"Newsfeed.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UITabBarItem *itemReq=[self.objTb.tabBar.items objectAtIndex:1];
itemReq.title=#"Request";
itemReq.image = [[UIImage imageNamed:#"request.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UITabBarItem *itemMsg=[self.objTb.tabBar.items objectAtIndex:2];
itemMsg.title=#"Messenger";
itemMsg.image = [[UIImage imageNamed:#"messenger.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UITabBarItem *itemNotification=[self.objTb.tabBar.items objectAtIndex:3];
itemNotification.title=#"Notification";
itemNotification.image = [[UIImage imageNamed:#"notificationBlue.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UITabBarItem *itemProfile=[self.objTb.tabBar.items objectAtIndex:4];
itemProfile.title=#"Profile";
itemNotification.image = [[UIImage imageNamed:#"profile.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

replace layout manager of uitextview

NSTextContainer on Mac OS X has a method replaceLayoutManager: to replace the NSLayoutManager of NSTextView with a subclass of NSLayoutManager.
Unfortunately iOS doesn't have such a function.
I tried a combination of these lines of code, but it keeps crashing.
THLayoutManager *layoutManager = [[THLayoutManager alloc] init];
[layoutManager addTextContainer:[self textContainer]];
// [[self textStorage] removeLayoutManager:[self layoutManager]];
//[[self textStorage] addLayoutManager:layoutManager];
[[self textContainer] setLayoutManager:layoutManager];
What is the correct procedure to replace the NSLayoutManager of an UITextview?
Since iOS9, NSTextContainer has the same method as macOS. So now you can replace the layout manager on your storyboard UITextView with your own subclass:
textView.textContainer.replaceLayoutManager(MyLayoutManager())
Have a look at the WWDC2013 Intro To Text Kit video and sample code where they show how to do it.
https://developer.apple.com/downloads/index.action?name=WWDC%202013
https://developer.apple.com/wwdc/videos/
Below is an extract from the code
-(void)viewDidLoad
{
[super viewDidLoad];
// our auto layout views use a design spec that calls for
// 8 pts on each side except the bottom
// since we scroll at the top here, only inset the sides
CGRect newTextViewRect = CGRectInset(self.view.bounds, 8., 0.);
self.textStorage = [[TKDInteractiveTextColoringTextStorage alloc] init];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
NSTextContainer *container = [[NSTextContainer alloc] initWithSize:CGSizeMake(newTextViewRect.size.width, CGFLOAT_MAX)];
container.widthTracksTextView = YES;
[layoutManager addTextContainer:container];
[_textStorage addLayoutManager:layoutManager];
UITextView *newTextView = [[UITextView alloc] initWithFrame:newTextViewRect textContainer:container];
newTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
newTextView.scrollEnabled = YES;
newTextView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
[self.view addSubview:newTextView];
self.textView = newTextView;
self.textStorage.tokens = #{ #"Alice" : #{ NSForegroundColorAttributeName : [UIColor redColor] },
#"Rabbit" : #{ NSForegroundColorAttributeName : [UIColor orangeColor] },
TKDDefaultTokenName : #{ NSForegroundColorAttributeName : [UIColor blackColor] } };
}

How can i select 2 icons for single item in a tab bar?

I have a tabBarController and i'm setting icons but i can not select the unselected icons successful. It seams i just select one 1 icon and a selected colour and that's it.
How can i pic a colour or set a separate icon for the not selected state?
I'm setting the icon and selected icon with no success.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Set the status bar to light style
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UITabBarController *tabBarController = (UITabBarController*)[mainStoryboard instantiateViewControllerWithIdentifier: #"TabBar"];
UITabBar *tabBar = tabBarController.tabBar;
NSLog(#"%#", tabBar.items);
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
//UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
//UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
tabBarItem1.image = [UIImage imageNamed:#"IconTabBarFolder"];
tabBarItem1.selectedImage = [UIImage imageNamed:#"IconTabBarFolder:Highlighted"];
[[UITabBarItem appearance] setTitleTextAttributes:#{NSFontAttributeName : [UIFont fontWithName:#"HelveticaNeue-Medium" size:10.0f], NSForegroundColorAttributeName : [UIColor colorWithRed:.5 green:.5 blue:.5 alpha:1] } forState:UIControlStateNormal];
return YES;
}
You can add images for UITabbarItem by using this code:
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"stores222.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"home-icon-inactive.png"]];
Update for iOS7:
please use this code :
UIImage *aImage = [UIImage imageNamed:#"a"];
aImage = [aImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *inaImage = [UIImage imageNamed:#"ina"];
inaImage = [inaImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.aController.tabBarItem = [[UITabBarItem alloc]initWithTitle:#"title" image:inaImage selectedImage:aImage];

tab bar item position not lined up with the background of the bar

Im having trouble trying to implement my own design to a tab bar. Somehow the items and background don't align correctly even tho they have the same height. The buttons seem to be further up than the background and i can't figure out why.
Code for implementing the items/buttons:
(this is added in my "viewDidLoad" in "Tab1ViewController.m", wich is the first of my 5 view controllers)
UIImage *selectedImage0 = [UIImage imageNamed:#"searchSEL"];
UIImage *unselectedImage0 = [UIImage imageNamed:#"search"];
UIImage *selectedImage1 = [UIImage imageNamed:#"homeSEL"];
UIImage *unselectedImage1 = [UIImage imageNamed:#"home"];
UIImage *selectedImage2 = [UIImage imageNamed:#"dareSEL"];
UIImage *unselectedImage2 = [UIImage imageNamed:#"dare"];
UIImage *selectedImage3 = [UIImage imageNamed:#"starSEL"];
UIImage *unselectedImage3 = [UIImage imageNamed:#"star"];
UIImage *selectedImage4 = [UIImage imageNamed:#"friendsSEL"];
UIImage *unselectedImage4 = [UIImage imageNamed:#"friends"];
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
UITabBarItem *item4 = [tabBar.items objectAtIndex:4];
[item0 setTitle:#""];
[item1 setTitle:#""];
[item2 setTitle:#""];
[item3 setTitle:#""];
[item4 setTitle:#""];
[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
[item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
[item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:unselectedImage2];
[item3 setFinishedSelectedImage:selectedImage3 withFinishedUnselectedImage:unselectedImage3];
[item4 setFinishedSelectedImage:selectedImage4 withFinishedUnselectedImage:unselectedImage4];
and to add the background i just added this code to my didFinishLaunchingWithOptions in my appDelegate.m
UITabBar *tabBar = [UITabBar appearance];
[tabBar setBackgroundImage:[UIImage imageNamed:#"bg"]];
The buttons are 64x49px and the background is 320x49px.
I wont be able to answer to or read comments in a while, but I will as soon as i can, hopefully someone can help.
Well, figured it out and it was kind of obvious. The part under the items are where the title should be. And since my icons just took up the icon space and not the title space, there was a emptiness under where the title would be.
Solved the problem by creating a whole background image for the bar and specified borders and gradient on that instead and just created the icons separately ass small white/black images and transparent background.

Can not adjust UIPopupController to display images

In my application (code listed below), I use a popover to display a series of colors that the user can choose. These colors are used for the color of the drawing they are completing above. I am trying to modify the popover to work the same way, except for this time I would want to display images (the images are saved in the application's documents folder as png files) instead of blocks of color. Listed below is the working code for the color selector popover. ColorGrid is a UIview which contains an NSArray Colors, as well as two NSUIntegers columnCount and rowCount. I have tried to replace the items in the colors array with UIImages of the png files, as well as UIImageViews but I have not been able to get a successful result (or a compilable one). Listed below is the working code. Could anyone show me how I can change the UIColor items to the images to show them in the grid?
- (IBAction)popoverStrokeColor:(id)sender {
StrokeColorController *scc = [[[StrokeColorController alloc] initWithNibName:#"SelectColorController" bundle:nil] autorelease];
scc.selectedColor = self.strokeColor;
[self doPopoverSelectColorController:scc sender:sender];
}
- (void)doPopoverSelectColorController:(SelectColorController*)scc sender:(id)sender {
[self setupNewPopoverControllerForViewController:scc];
scc.container = self.currentPopover;
self.currentPopover.popoverContentSize = scc.view.frame.size;
scc.colorGrid.columnCount = 2;
scc.colorGrid.rowCount = 3;
scc.colorGrid.colors = [NSArray arrayWithObjects:
//put the following below back in after testing
[UIColor blackColor],
[UIColor blueColor],
[UIColor redColor],
[UIColor greenColor],
[UIColor yellowColor],
[UIColor orangeColor],
//[UIColor purpleColor],
// [UIColor brownColor],
// [UIColor whiteColor],
// [UIColor lightGrayColor],
//[UIColor cyanColor],
//[UIColor magentaColor],
nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(colorSelectionDone:) name:ColorSelectionDone object:scc];
[self.currentPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; //displays the popover and anchors it to the button
}
Thanks for your help. I am new to objective-c.
edit - heres the function with my attempt to insert the images instead of the colors
- (void)doPopoverSelectColorController:(SelectColorController*)scc sender:(id)sender {
[self setupNewPopoverControllerForViewController:scc];
scc.container = self.currentPopover;
self.currentPopover.popoverContentSize = scc.view.frame.size;
// these have to be set after the view is already loaded (which happened
// a couple of lines ago, thanks to scc.view...
scc.colorGrid.columnCount = 2;
scc.colorGrid.rowCount = 3;
//here we need to get the UIImage items to try to put in the array.
NSArray *pathforsave = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [pathforsave objectAtIndex:0];
//here we need to add the file extension onto the file name before we add the name to the path
//[fileName appendString:#".hmat"];
NSString *strFile = [documentDirectory stringByAppendingPathComponent:#"test.png"];
NSString *strFile1 = [documentDirectory stringByAppendingPathComponent:#"test1.png"];
NSString *strFile2 = [documentDirectory stringByAppendingPathComponent:#"test2.png"];
NSString *strFile3 = [documentDirectory stringByAppendingPathComponent:#"test3.png"];
NSString *strFile4 = [documentDirectory stringByAppendingPathComponent:#"test4.png"];
NSString *strFile5 = [documentDirectory stringByAppendingPathComponent:#"test5.png"];
//now for the Images
UIImage *image = [ UIImage imageWithContentsOfFile: strFile];
UIImage *image1 = [ UIImage imageWithContentsOfFile: strFile1];
UIImage *image2 = [ UIImage imageWithContentsOfFile: strFile2];
UIImage *image3 = [ UIImage imageWithContentsOfFile: strFile3];
UIImage *image4 = [ UIImage imageWithContentsOfFile: strFile4];
UIImage *image5 = [ UIImage imageWithContentsOfFile: strFile5];
UIImageView *imageview = [[[UIImageView alloc] initWithImage:image] autorelease];
[self.view addSubview:imageview];
UIImageView *imageview1 = [[[UIImageView alloc] initWithImage:image1] autorelease];
[self.view addSubview:imageview1];
UIImageView *imageview2 = [[[UIImageView alloc] initWithImage:image2] autorelease];
[self.view addSubview:imageview2];
UIImageView *imageview3 = [[[UIImageView alloc] initWithImage:image3] autorelease];
[self.view addSubview:imageview3];
UIImageView *imageview4 = [[[UIImageView alloc] initWithImage:image4] autorelease];
[self.view addSubview:imageview4];
UIImageView *imageview5 = [[[UIImageView alloc] initWithImage:image5] autorelease];
[self.view addSubview:imageview5];
imageview.image = image;
imageview1.image = image1;
imageview2.image = image2;
imageview3.image = image3;
imageview4.image = image4;
imageview5.image = image5;
scc.colorGrid.colors = [NSArray arrayWithObjects:
// When attempting to add the images like this - get the error identified expected
// after the e in image, at the end bracket. Putting a * does nothing to change the error
[image],
// When adding one of the Imageviews, i get the same error as above
//below is how I attempted to add it
[imageView],
//
nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(colorSelectionDone:) name:ColorSelectionDone object:scc];
[self.currentPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; //displays the popover and anchors it to the button
}
Remove your square brackets around image and/or imageView :
scc.colorGrid.colors = [NSArray arrayWithObjects:
// Not : [image] but
image,
// or
imageView,
// Not : [imageView],
nil];