在XCode 6.4中设置TabBar项目标题和选择图像

时间:2015-07-28 10:09:41

标签: ios objective-c uitabbarcontroller

我正在使用xcode 6.4并使用storyboard来设置Tab栏控制器项目及其图像。

问题1 - 在标签栏项目选择的故事板中,已有条形图项和图像选择选项。我已在两个字段上分配了图像,但图像在选择时没有变化。仅存在默认(黑色)图像。

我已经提到了Link。然后还存在默认(黑色)

第2期 - 在故事板中,我已经在条形图和图像选择上分配了所有图像并使用了给定的代码,然后在标签栏项目中看不到标题。只有图像在选择时工作正常。

代码 -

- (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];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
UITabBarItem *tabBarItem5 = [tabBar.items objectAtIndex:4];

tabBarItem1.title = @"Home";
tabBarItem2.title = @"Search";
tabBarItem3.title = @"Cart";
tabBarItem4.title = @"Account";
tabBarItem5.title = @"More";



[tabBarItem1 setSelectedImage:[[UIImage imageNamed:@"HomeSelected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

[tabBarItem2 setSelectedImage:[[UIImage imageNamed:@"SearchOpened.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

[tabBarItem3 setSelectedImage:[[UIImage imageNamed:@"CartOpened.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

[tabBarItem4 setSelectedImage:[[UIImage imageNamed:@"ProfileOpened.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

[tabBarItem5 setSelectedImage:[[UIImage imageNamed:@"MoreTabOpened.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];



// Change the tab bar background
UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected.png"]];

// Change the title color of tab bar items
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                   [UIColor whiteColor], NSForegroundColorAttributeName,
                                                   nil] forState:UIControlStateNormal];
UIColor *titleHighlightedColor = [UIColor colorWithRed:255.0/255.0 green:109.0/255.0 blue:0.0/255.0 alpha:1.0];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                   titleHighlightedColor, NSForegroundColorAttributeName,
                                                   nil] forState:UIControlStateHighlighted];
return YES;
}

我已经看过iOS Docs,但找不到有关这些问题的任何有用的更新。请更新我。

1 个答案:

答案 0 :(得分:0)

您应该创建自定义标签栏

有用的链接:http://swiftiostutorials.com/tutorial-custom-tabbar-storyboard/