我试图改变我未选中的选项卡栏图标图像的图标色调。我已经使用了下面的补丁代码,但是,我发现这个补丁代码的帖子说在标签栏控制器中运行这个,但我不知道如何做到这一点,所以我将它运行在 - (void)viewDidLoad方法中视图控制器.m文件。它提出了一个错误,说'属性'tabBar'没有在'ViewController *'类型的对象上找到'“我该如何解决这个问题?如何在tabbarcontroller中运行代码
// set color of selected icons and text to red
self.tabBar.tintColor = [UIColor redColor];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
// set color of unselected text to green
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], NSForegroundColorAttributeName, nil]
forState:UIControlStateNormal];
// set selected and unselected icons
UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];
// this way, the icon gets rendered as it is (thus, it needs to be green in this example)
item0.image = [[UIImage imageNamed:@"unselected-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// this icon is used for selected tab and it will get tinted as defined in self.tabBar.tintColor
item0.selectedImage = [UIImage imageNamed:@"selected-icon.png"];
你在哪里创建'UITabBarController'?在Interface Builder中? – derpoliuk
如果您使用xcode模板,则tabbarcontroller位于您的appdelegate中。 – Hackmodford
@StasDerpoliuk好的事情是,我只是把一个标签栏放在我的主视图控制器上,默认显示在故事板中。我应该删除这个视图控制器,并把一个uitabbarcontroller? – ian