我正在创建一个包含5个标签项的标签栏。我以编程方式创建了tabbar。我想将默认图像(更多)设置为tabbar项目。如果我通过IB创建tabbar,我会选择标识符为“More”,但是如何以编程方式进行?
答案 0 :(得分:0)
为此,您需要创建一个父类为UITabBar的类。
这是它的.h文件:
#import <UIKit/UIKit.h>
@interface ImageTabBar : UITabBar
{
}
@end
这是它的.m文件:
#import "ImageTabBar.h"
#import "GlobalVars.h"
@implementation ImageTabBar
- (void) drawRect:(CGRect)rect
{
UIImage *tabImage;
switch (intTabBarSelectedIndex)
{
case 0:
tabImage=[UIImage imageNamed:@"TabBarImageTwitter.png"];
[tabImage drawAtPoint:CGPointMake(0, 0)];
break;
case 1:
tabImage=[UIImage imageNamed:@"TabBarImageCalender.png"];
[tabImage drawAtPoint:CGPointMake(0, 0)];
break;
case 2:
tabImage=[UIImage imageNamed:@"TabBarImageStanding.png"];
[tabImage drawAtPoint:CGPointMake(0, 0)];
break;
case 3:
tabImage=[UIImage imageNamed:@"TabBarImageNews.png"];
[tabImage drawAtPoint:CGPointMake(0, 0)];
break;
case 4:
tabImage=[UIImage imageNamed:@"TabBarImagePhotos.png"];
[tabImage drawAtPoint:CGPointMake(0, 0)];
break;
default:
break;
}
}
- (void)dealloc
{
[super dealloc];
}
@end
声明中:
switch (intTabBarSelectedIndex)
intTabBarSelectedIndex将被定义为全局变量,并且是整数类型。
现在来到TabBarController类。
这是它的.h文件
#import <UIKit/UIKit.h>
@class ImageTabBar;
@interface TabBarViewController : UIViewController <UITabBarControllerDelegate>
{
ImageTabBar *objOfImageTabBar;
IBOutlet UITabBarController *uiTabBarC;
}
@property (nonatomic, retain) IBOutlet UITabBarController *uiTabBarC;
- (void)setNeedsDisplay;
@end
这是它的.m文件
在TabBarController类'.m文件中包含以下函数:
- (void)viewDidLoad
{
[self.navigationController setNavigationBarHidden:YES];
self.view = uiTabBarC.view;
uiTabBarC.selectedIndex = intTabBarSelectedIndex;
uiTabBarC.delegate = self;
[super viewDidLoad];
}
#pragma mark TaBarViewController delegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
intTabBarSelectedIndex = uiTabBarC.selectedIndex;
UITabBar *aTabBar = tabBarController.tabBar;
[aTabBar setNeedsDisplay];
}
- (void)setNeedsDisplay
{
}
完全按照这样做,您的代码将完美运行。