嗨,我正在 xcode 中创建标签栏控制器,而不是在界面生成器中。选项卡中视图的标题设置选项卡中的标题,但我不确定如何设置图像。
任何人都可以帮忙吗?
嗨,我正在 xcode 中创建标签栏控制器,而不是在界面生成器中。选项卡中视图的标题设置选项卡中的标题,但我不确定如何设置图像。
任何人都可以帮忙吗?
我发现您可以获取视图控制器数组,然后添加图像:
NSArray *tabs = tabBarController.viewControllers;
UIViewController *tab1 = [tabs objectAtIndex:0];
tab1.tabBarItem.image = [UIImage imageNamed:@"clockicon.png"];
UIViewController *tab2 = [tabs objectAtIndex:1];
tab2.tabBarItem.image = [UIImage imageNamed:@"nearest.png"];
UIViewController 有一个tabBarItem属性,它有一个image属性(继承自UIBarItem类UITabBarItem子类)。例如:
viewController.tabBarItem.image = [UIImage imageNamed:@"foo.png"];
UITabBarController
不允许许多特征,例如标签栏项目的彩色图像、栏项目的字体以及标签栏的背景图像。我找到了解决办法。当一个通过 xibUITabBarController
添加到UITabBarController
(不是应用程序委托)时,还有一个UIView
文件UITabBarController
(名称为“视图”)。为标签栏以及标签栏项目、标签和任何你想要的东西添加图像视图,使它们正好落在标签栏的框架下。现在我们必须使 tab bat 不可见但仍然可以使用。减少标签栏的 alpha 值UITabBarController
从 xib tp 0.02(低于 0.01 的标签栏不捕捉触摸动作)。你完成了!即使以编程方式完成所有这些......这些都是非常微不足道的......这个迂回的旅程避免了开发人员不能继承的苹果政策UITabBarController
。所以......干杯:)
我快速编写了以下类,并从 UITabBarController 显示/隐藏选项卡视图就像魔术一样工作:
TabBarDesigner.h
#import <Foundation/Foundation.h>
@interface TabBarDesigner : NSObject
{
}
+(void) setTabBarController:(UITabBarController *)tabBarController
items:(NSArray *)tabBarItems
viewControllers:(NSArray *)viewControllers;
+(void) removeItemsInRange:(NSRange) range;
@end
TabBarDesigner.m
#import "TabBarDesigner.h"
static NSArray *_tabBarItems = NULL;
static NSArray *_viewControllers = NULL;
static UITabBarController *_tabBarController = NULL;
@implementation TabBarDesigner
+(void) setTabBarController:(UITabBarController *)tabBarController
items:(NSArray *)tabBarItems
viewControllers:(NSArray *)viewControllers
{
if (tabBarItems && viewControllers && tabBarController)
{
if ([tabBarItems count] == [viewControllers count])
{
[_tabBarItems release];
[_viewControllers release];
_tabBarItems = [tabBarItems copy];
_viewControllers = [viewControllers copy];
_tabBarController = tabBarController;
}
}
}
+(void) removeItemsInRange:(NSRange) range
{
if (_tabBarController)
{
if ( range.location < ([_tabBarItems count] - 1) )
{
if ( (range.length + range.location) < [_tabBarItems count] )
{
NSMutableArray *tabBarItems = [_tabBarItems mutableCopy];
[tabBarItems removeObjectsInRange:range];
NSMutableArray *viewControllers = [_viewControllers mutableCopy];
[viewControllers removeObjectsInRange:range];
[_tabBarController setViewControllers:viewControllers];
NSUInteger i;
for (i = 0; i< [viewControllers count]; i++)
{
UIViewController *vC = [viewControllers objectAtIndex:i];
vC.tabBarItem.image = [[tabBarItems objectAtIndex:i] image];
vC.tabBarItem.title = [[tabBarItems objectAtIndex:i] title];
vC.tabBarItem.tag = [[tabBarItems objectAtIndex:i] tag];
}
[tabBarItems release];
[viewControllers release];
}
}
}
}
@end
如何使用此类的示例:在您的 MyAppDelegate.m
#import "TabBarDesigner.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[TabBarDesigner setTabBarController:_tabBarController
items:[_tabBarController.tabBar items]
viewControllers:[_tabBarController viewControllers]];
// remove the first 3 tabs
[TabBarDesigner removeItemsInRange:NSMakeRange(0,3)];
// show all tabs
[TabBarDesigner removeItemsInRange:NSMakeRange(0,0)];
// continue with your code
}
干杯!
斯威夫特 5.4
let tabs = tabBarController.viewControllers
let tab1 = tabs[0]
tab1.tabBarItem.image = UIImage(named: "clockicon.png")
let tab2 = tabs[1]
tab2.tabBarItem.image = UIImage(named: "nearest.png")