ios – 通过代码将UITabBar和tabbaritems添加到UITabBar(注意:我不想实现TabBarController)

我搜索了很多,相信我,我遇到了很多问题和示例代码,但没有一个符合我的要求,很疯狂怎么没有答案.

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

现在我做的是

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

现在我被困在如何将tabbar项添加到此UITabBar.

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

解决方法

您可以通过为标签栏创建所需的项目,将它们添加到数组,然后调用UITabBar方法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];

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...