2013-09-26 41 views
-1

我试图改变我未选中的选项卡栏图标图像的图标色调。我已经使用了下面的补丁代码,但是,我发现这个补丁代码的帖子说在标签栏控制器中运行这个,但我不知道如何做到这一点,所以我将它运行在 - (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"]; 
+0

你在哪里创建'UITabBarController'?在Interface Builder中? – derpoliuk

+0

如果您使用xcode模板,则tabbarcontroller位于您的appdelegate中。 – Hackmodford

+0

@StasDerpoliuk好的事情是,我只是把一个标签栏放在我的主视图控制器上,默认显示在故事板中。我应该删除这个视图控制器,并把一个uitabbarcontroller? – ian

回答

0

您可以从故事板中删除您的UIViewController,并在那里添加UITabBarController并将其设置为Initial View Controller

然后你就可以在AppDelegate.m这样访问此UITabBarController

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;

这是快速的方式。

您也可以继承UITabBarController的子类,在故事板中设置此类,并将所有代码放入此子类中。 (因为null之前说过15分钟:)

1

而不是使用的代码补丁,尝试做下面的VC:

[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"item_seleted.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"item_unselected.png"]]; 

编辑:

代码继承UITabBarController当上面使用,所以如果你insis你需要继承'UITabBarController',在故事板中更改tabBarController的类,并将该代码块放入其viewDidLoad

+0

谢谢null,但我想改变我的未选定图标的图标色调 – ian

+0

好吧,这将做的工作,它会改变未选定的选项卡图标,通过你不能改变色彩的方式,你必须改变图标为去做。 – null

+0

它说它已在ios7中折旧 – ian