简体   繁体   English

当tabbar不是root视图时更改tabbar图像

[英]changing tabbar images when tabbar not root view

I am looking at changing the graphics on a tabbar, such as 我正在寻找更改tabbar上的图形,例如

- (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.title = @"Home";
tabBarItem2.title = @"Maps";
tabBarItem3.title = @"My Plan";
tabBarItem4.title = @"Settings";

The problem I have here is that my tabbarcontroller is not my root view, so how can I reference the tabbarcontroller to change the tab images? 我在这里遇到的问题是我的tabbarcontroller不是我的根视图,所以如何引用tabbarcontroller来更改选项卡图像?

I am following a suggestion from this post ( Can I have more than 1 UITabBarController? ) that refers to having a tableview that links to one or more tabbarcontrollers. 我正在关注这篇文章的建议( 我可以有多个UITabBarController? ),它指的是一个链接到一个或多个tabbarcontrollers的tableview。

So my root view is not a tab bar, but the tab bar view is loaded after coming from one previous screen. 所以我的根视图不是标签栏,但标签栏视图是在来自前一个屏幕之后加载的。

I have this all working, with the initial screen and then the tab bar, and everything is working fine, I just need to change the graphics on the tab bar, and am not able to do this as all tutorials on changing tab bar graphics use the app delegate and refer to the tabbarcontroller as the root view. 我有这个工作,初始屏幕,然后是标签栏,一切都工作正常,我只需要更改标签栏上的图形,我不能这样做,因为所有关于更改标签栏图形的教程使用app委托并将tabbarcontroller称为根视图。

Any help on this greatly appreciated! 对此的任何帮助非常感谢!

Tab bar items belong to the individual content view controllers in each tab, so rather than trying to reference the tab bar controller, you should change the tab item properties in those controllers. 选项卡栏项属于每个选项卡中的各个内容视图控制器,因此您应该更改这些控制器中的选项卡项属性,而不是尝试引用选项卡栏控制器。 If you want those changes to be visible as soon as the tab bar controller comes on screen, you need to put those customizations in their awakeFromNib or initWithCoder methods (for controllers set up in IB). 如果您希望一旦标签栏控制器出现在屏幕上,这些更改就会显示,您需要将这些自定义设置放在其awakeFromNib或initWithCoder方法中(对于在IB中设置的控制器)。

Richard, 理查德,

Firstly, the post you reference pointed out that this is very bad visual design, but anyway, if you need to do it. 首先,你引用的帖子指出这是非常糟糕的视觉设计,但无论如何,如果你需要这样做。

How you reference the tab bar controller depends on how you instantiate it. 如何引用标签栏控制器取决于您如何实例化它。 This stuff should be fairly obvious, so I'm guessing that you're working with xibs or storyboards. 这个东西应该是相当明显的,所以我猜你正在使用xib或故事板。

Either way, when you load the second view that is going to have the tab bar on it, you'll be loading a view controller to control the view. 无论哪种方式,当您加载第二个视图,其中将有标签栏,您将加载视图控制器来控制视图。 Just bind the tab bar to a property of the view controller in IB, and you can do your set up in the view did load of the view controller. 只需将标签栏绑定到IB中视图控制器的属性,您就可以在视图中设置视图控制器的负载。

Actually, if you're using IB ( XIB or storyboards), you can just set up your icons there. 实际上,如果你正在使用IB(XIB或故事板),你可以在那里设置你的图标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM