本文介绍了通过代码将UITabBar和tabbaritems添加到UITabBar(注意:我不想实现TabBarController)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常搜索并相信我,我遇到了很多问题和示例代码,但没有一个符合我的要求,但是如果没有这个问题的答案那就太疯狂了。

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.

我的要求具体: -
1.我想要UITabBar而不是UITabBarController
2.我希望它位于我的UIView
3的右上角。我想要两个标签栏项目(不需要与两个视图控制器相关联)

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

现在我做的是

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

现在我被困在如何向这个UITabBar添加tabbar项目。

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

我想这可能很简单,但我找到的每个代码都添加了TabBarController,但我不想使用tabbarcontroller作为启动器,它只出现在屏幕的底部,加上不是要求。

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.

推荐答案

您可以通过为标签栏创建所需的项目,将它们添加到数组中来实现此目的,以及然后调用UITabBar方法 setItems:animated:

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];

这篇关于通过代码将UITabBar和tabbaritems添加到UITabBar(注意:我不想实现TabBarController)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 06:57