我正在自定义我的 TabBar 图标,我遇到了一个问题,即自定义选项卡图形直到第一次被单击时才会显示,第一个选项卡除外。设置如下Entry->VC->Tab Controller->TabVC1->TabVC2->TabVC3->TabVC4->TabVC5。让它们在选项卡 Controller 首次初始化时显示的最佳方法是什么。
** 这些属性是在我进入选项卡 Controller 时设置的(它们工作正常)
// Set background to white for the tab bar
UIImage *tabBackground = [[UIImage imageNamed:@"tabback.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UITabBar appearance] setBackgroundImage:tabBackground];
// setting the selected color to blue
self.tabBar.tintColor = [UIColor blueColor];
// changing the tab bar text color
[[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIColor blackColor], UITextAttributeTextColor,[UIFont fontWithName:@"Copperplate-Bold" size:0.0], UITextAttributeFont, nil] forState:UIControlStateNormal];
** 这些是在各个 TabVC 中设置的(它们仅在第一次单击其选项卡后显示)
// loading the custom icon for front and back
UITabBarItem *tabicon = [[UITabBarItem alloc] initWithTitle:@"Daily" image:[UIImage imageNamed:@"Day.png"] tag:0];
[tabicon setFinishedSelectedImage:[UIImage imageNamed:@"Day.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"Day.png"]];
[self setTabBarItem:tabicon];
最佳答案
首先你的标签栏 Controller 应该是你的 Root View Controller 。也就是说,您的窗口的 rootViewController。 来自documentation :
When deploying a tab bar interface, you must install this view as the root of your window. Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller.
此外,由于您调用了 self.tabBar
,我感觉您正在子类化 UITabBarController。
文档建议也反对这一点。
话虽如此,您可以在首次设置外观时设置标签栏项目(第一个代码片段)。
例如,要更改第二个 View Controller 选项卡项目,您会这样做
// loading the custom icon for front and back
UITabBarItem *tabicon = [[UITabBarItem alloc] initWithTitle:@"Daily" image:[UIImage imageNamed:@"Day.png"] tag:0];
[tabicon setFinishedSelectedImage:[UIImage imageNamed:@"Day.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"Day.png"]];
UIViewController *second = [self.viewControllers objectAtIndex:1];
[second setTabBarItem:tabicon];
重点是您需要在标签栏 View 出现之前设置自定义外观。
关于ios - 具有自定义图形的 xcode 选项卡 Controller ,在第一次单击之前不会加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14172168/