以编程方式设置自定义 UITabBarItem?

编程入门 行业动态 更新时间:2024-10-09 00:48:14
本文介绍了以编程方式设置自定义 UITabBarItem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

在 iOS 中,TabBarController 中的 TabBar 属性是只读的.如何将自定义项目与特定视图控制器相关联?如何访问 tabBar 内的 UITabBarItems?

In iOS, the TabBar property in the TabBarController is read only. How can I associate a custom item with a particular view controller? How do I access the UITabBarItems inside the tabBar?

像这样

CustomView *custom = [[CustomView alloc] init];
UITabBarItem *customTab = [[UITabBarItem alloc] initWithTitle:@"Custom" image:[UIImage imageNamed:@"custom.png"] tag:0];
SecondView *second = [[SecondView alloc] init];
UITabBarItem *secondTab = [[UITabBarItem alloc] initWithTitle:@"Next" image:[UIImage imageNamed:@"next.png"] tag:1];
NSArray *views = [NSArray arrayWithObjects:custom,second,nil];
[tabBarController setViewControllers:views];
//how do I set the individual TabBarItems (customTab,secondTab) to be associated
//with the views in question?  tabBarController.tabBar is read only

推荐答案

在每个视图控制器中,你可以设置一个 tabBarItem 属性.如果视图控制器归 UITabBarViewController 所有,则标签栏上的关联项将相应更新.

Inside each view controller, you can set a tabBarItem property. If the view controller is owned by a UITabBarViewController the associated item on the tab bar will be updated accordingly.

像这样

-(void)viewDidLoad {
    [super viewDidLoad];
    UITabBarItem *tbi = [[UITabBarItem alloc] initWithTitle:yourTitle image:yourIcon tag:yourTag];
    [self setTabBarItem:tbi]
    [tbi release];
}

很明显,您不限于在 viewDidLoad 方法中执行此操作.

You are not restricted to perform this operation in the viewDidLoad method, obviously.

这篇关于以编程方式设置自定义 UITabBarItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-29 01:02:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1182555.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   方式   UITabBarItem

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!