Adding a UITabBar and tabbaritems to the UITabBar through code ( NOTE: I don't want to implement TabBarController )

余生颓废 提交于 2019-12-21 04:53:43

问题


I have searched a lot and beleive me i have come across many questions and sample codes but none of them fit my requirement , it's crazy how can there not be an answer for this.

My requirements specifically:- 1. I want a UITabBar not a UITabBarController 2. I want it to be on the top-right corner of my UIView 3. I want two tab bar items on it( need not be assiciated with two view controllers)

Now what i did was

UITabBar *myTabBar=[UITabBar alloc]initWithFrame:myTabFrame];
[self.view addSubView:myTabBar];
myTabBar.delegate=self;

Now i am stuck at how do i add tabbar items to this UITabBar.

I guess it's probably easy but every code i found has TabBarController added to it, but i don't want to use tabbarcontroller as for starters it only comes on the bottom of the screen , plus that's not the requirement.


回答1:


You can do this by creating the items you want for your tab bar, adding them to an array, and then calling the UITabBar method setItems:animated:

UITabBarItem *firstItem = [[UITabBarItem alloc] initWithTitle:@"First" image:firstImage tag:1];
UITabBarItem *secondItem = [[UITabBarItem alloc] initWithTitle:@"Second" image:secondImage tag:2];

NSArray *itemsArray = @[firstItem, secondItem];

[myTabBar setItems:itemsArray animated:YES];


来源:https://stackoverflow.com/questions/10408111/adding-a-uitabbar-and-tabbaritems-to-the-uitabbar-through-code-note-i-dont-w

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