Setting custom UITabBarItem programmatically?

為{幸葍}努か 提交于 2019-12-12 19:03:55

问题


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?

Like this

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

回答1:


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.

Something like this

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

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



来源:https://stackoverflow.com/questions/3907926/setting-custom-uitabbaritem-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!