@implementation MyNav (一个合用navigationbar和tabbar的类)
修改tabbar的背景图片
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- //设置tabbar的背景图片
- UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbar.png"]];
- img.frame = CGRectMake(0, 0,self.tab.tabBar.frame.size.width,self.tab.tabBar.frame.size.height);
- img.contentMode = UIViewContentModeScaleToFill;
- [self.tab.tabBar insertSubview:img atIndex:1];
- [img release];
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
修改navbar的背景图片
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- UIViewController *viewCon1, *viewCon2, *viewCon3, *viewCon4;
- viewCon1 = [[NextViewController alloc] init];
- viewCon2 = [[NextViewController alloc] init];
- viewCon3 = [[NextViewController alloc] init];
- viewCon4 = [[NextViewController alloc] init];
- self.tab = [[UITabBarController alloc] init];
- //设置5个nav
- UINavigationController *nav1, *nav2, *nav3, *nav4;
- nav1 = [[[UINavigationController alloc] initWithRootViewController:viewCon1] autorelease];
- [nav1.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
- nav2 = [[[UINavigationController alloc] initWithRootViewController:viewCon2] autorelease];
- [nav2.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
- nav3 = [[[UINavigationController alloc] initWithRootViewController:viewCon3] autorelease];
- [nav3.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
- nav4 = [[[UINavigationController alloc] initWithRootViewController:viewCon4] autorelease];
- [nav4.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
- //释放内存
- [viewCon1 release];
- [viewCon2 release];
- [viewCon3 release];
- [viewCon4 release];
- self.tab.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nav3,nav4,nil];
- [self.view setFrame:[UIScreen mainScreen].bounds];
- [self.view addSubview:self.tab.view ];
- }
tabitem被选中时一个图片,不被选中时为另一张图片
@implementation NextViewController
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- self.title = @"全文数据库";
- UIImage* img = [UIImage imageNamed:@"user.png"];
- UITabBarItem *tabItem = [[UITabBarItem alloc] initWithTitle:@"MeGa"
- image:img
- tag:0];
- self.tabBarItem = tabItem;
- [tabItem release];
- UIImage* img2 = [UIImage imageNamed:@"user.png"];
- UIImage* img1 = [UIImage imageNamed:@"star.png"];
- [self.tabBarItem setFinishedSelectedImage:img1 withFinishedUnselectedImage:img2];
- }
- return self;
- }