xcode 使用 StoryBoards 以编程方式设置 UITabBarItem 图标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8761468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:55:26  来源:igfitidea点击:

Programmatically setting UITabBarItem icons with StoryBoards?

iphonexcodeios5

提问by Will Larche

I want to use a UITabBarSystemItem for the icon of one of my tabBarItem's but I'm using storyboards. I'm not sure where to set it. If I set it in the view's viewDidLoad then it doesn't change until you push the button in the tabBar. Before that it's just the blue ? square.

我想使用 UITabBarSystemItem 作为我的 tabBarItem 之一的图标,但我使用的是故事板。我不确定在哪里设置它。如果我在视图的 viewDidLoad 中设置它,那么它不会改变,直到你按下 tabBar 中的按钮。在此之前,它只是蓝色?正方形。

And as far as I know, you can't use UITabBarSystemItems in IB inspector.

据我所知,您不能在 IB 检查器中使用 UITabBarSystemItems。

UPDATE:

更新:

Well first of all I'm an idiot. You can totally choose the icon in the IB Inspector.

好吧,首先我是个白痴。您完全可以在 IB Inspector 中选择图标。

Tab Bar Item -> Identifier -> Choose your icon.

标签栏项目 -> 标识符 -> 选择你的图标。

But the question still remains how to do it programmatically. Or rather when/where?

但问题仍然是如何以编程方式做到这一点。或者更确切地说是何时/何地?

回答by John F

When using storyboards, a view controller initializes using the initWithCoder constructor so you could override that function and set the system item icon there like so

使用故事板时,视图控制器使用 initWithCoder 构造函数进行初始化,因此您可以覆盖该函数并像这样设置系统项图标

- (id)initWithCoder:(NSCoder*)aDecoder 
{
    if(self = [super initWithCoder:aDecoder]) 
    {
        self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
    }
    return self;
}

Naturally, you can change the value of the system item to any of the values supported. Apple lists them here

当然,您可以将系统项的值更改为支持的任何值。苹果在这里列出了它们

回答by iCreative

Check this code for your problem: Its working in my app.

检查此代码是否存在您的问题:它在我的应用程序中工作。

- (NSArray *) initializeViewControllers
{
    NSArray *viewControllerArray = nil;
    viewController1 = <View Init Code>
    viewController2 = <View Init Code>
    viewController3 = <View Init Code>

    1stNavController = [[UINavigationController alloc] initWithRootViewController:viewController1]; 
    UIImage *img = [UIImage imageNamed:@"tab_home"];
    [1stNavController .tabBarItem initWithTitle:@"Home" image:img tag:1];

    2ndNavController = [[UINavigationController alloc] initWithRootViewController:viewController2]; 
    img = [UIImage imageNamed:@"tab_timeDrop"];
    [2ndNavController .tabBarItem initWithTitle:@"Time Entry" image:img tag:2];

    3rdNavController = [[UINavigationController alloc] initWithRootViewController:viewController3];

    img = [UIImage imageNamed:@"tab_invoiceSummary"];
    [3rdNavController.tabBarItem initWithTitle:@"Invoice Summary" image:img tag:3];

    viewControllerArray = [NSArray arrayWithObjects:1stNavController,2ndEntryNavController,3rdReportNavController, nil];
    return viewControllerArray;
}

This code is returning View Controllers with Images for their respective tabs. Here i used Navigation Controller inside the tabbar controller. you can also use view controller instead of navigation Controller.

此代码为各自的选项卡返回带有图像的视图控制器。在这里,我在标签栏控制器中使用了导航控制器。您还可以使用视图控制器代替导航控制器。

Just add this code and initialize your tabbar controller as follows inside appdidfinishlaunchingmethod:

只需添加此代码并在appdidfinishlaunching方法中按如下方式初始化您的标签栏控制器:

 tabbarController = [[UITabBarController alloc] init];

_tabbarController.viewControllers = [self initializeViewControllers];

self.window.rootViewController = tabbarController;

Hope it works.

希望它有效。

Please reply.

请回复。

回答by s.muellner

Well that's quite simple!

嗯,这很简单!

For the where -> create a custom TabBarController (e.g. WBTabBarController). In your StoryBoard explicitly set it.

对于 where -> 创建自定义 TabBarController(例如 WBTabBarController)。在您的 StoryBoard 中明确设置它。

For the how -> just overwrite viewDidLoad of the WBTabBarController and set your images for the TabBarItems. In the following example this was done using the NIKFontAwesomeIconFactory:

对于如何 -> 只需覆盖 WBTabBarController 的 viewDidLoad 并为 TabBarItems 设置图像。在以下示例中,这是使用 NIKFontAwesomeIconFactory 完成的:

#import "WBTabBarController.h"
#import "NIKFontAwesomeIcon.h"
#import "NIKFontAwesomeIconFactory.h"
#import "NIKFontAwesomeIconFactory+iOS.h"

@interface WBTabBarController ()

@end

@implementation WBTabBarController


- (void)viewDidLoad
{
    [super viewDidLoad];

    NIKFontAwesomeIconFactory *factory = [NIKFontAwesomeIconFactory tabBarItemIconFactory];

    UITabBarItem *item0 = self.tabBar.items[0];
    item0.image = [factory createImageForIcon:NIKFontAwesomeIconApple];

    UITabBarItem *item1 = self.tabBar.items[1];
    item1.image = [factory createImageForIcon:NIKFontAwesomeIconStackOverflow];
}

@end

回答by jrturton

Assuming the initial scene of your storyboard is the tab bar view controller, you can access it in applicationDidFinishLaunching, it is the rootViewControllerof the app delegate's window. I don't know if you can swap in a system item if the bar item is already present, but you can definitely set the image and title.

假设故事板的初始场景是标签栏视图控制器,您可以在 applicationDidFinishLaunching 中访问它,它是rootViewController应用程序委托的window. 如果栏项目已经存在,我不知道您是否可以交换系统项目,但您绝对可以设置图像和标题。