废话不多说,直接代码了:
.h文件主要代码:
@property(nonatomic,retain) YsmagNavigationViewController *ysamgnavigation1;
@property(nonatomic,retain) YsmagNavigationViewController *ysamgnavigation2;
@property(nonatomic,retain) YsmagNavigationViewController *ysamgnavigation3;
@property(nonatomic,retain) YsmagNavigationViewController *ysamgnavigation4;
@property(nonatomic,retain) UITabBar *tabBar;
@property (nonatomic,assign) NSInteger selectedIndex;
@property(nonatomic,retain) NSArray *arrayViewcontrollers;
.m文件主要代码:
_tabBar = [[UITabBar alloc] init];
_tabBar.backgroundImage = [UIImage imageNamed:@"bottom_bg"];
_tabBar.delegate = self;
UIImage *img =[[UIImage alloc]init];
_tabBar.selectionIndicatorImage = img;
[img release];
[self.view addSubview:_tabBar];
UITabBarItem *barItem1 = [[UITabBarItem alloc] initWithTitle:@"首页" image:nil tag:0];
[barItem1 setImage:[UIImage imageNamed:@"001_1.png"]];
[barItem1 setSelectedImage:[UIImage imageNamed:@"001"]];
UITabBarItem *barItem2 = [[UITabBarItem alloc] initWithTitle:@"歌曲" image:nil tag:1];
[barItem2 setFinishedSelectedImage:[UIImage imageNamed:@"002.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"002_2.png"]];
UITabBarItem *barItem3 = [[UITabBarItem alloc] initWithTitle:@"音乐" image:nil tag:2];
[barItem3 setFinishedSelectedImage:[UIImage imageNamed:@"003.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"003_3.png"]];
UITabBarItem *barItem4 = [[UITabBarItem alloc] initWithTitle:@"设置" image:nil tag:3];
[barItem4 setFinishedSelectedImage:[UIImage imageNamed:@"004.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"004_4.png"]];
NSArray *items = [[NSArray alloc]initWithObjects:barItem1,barItem2,barItem3,barItem4,nil];
[_tabBar setItems:items];
self.arrayViewcontrollers = [[self getViewControllers] retain];
[self tabBar:_tabBar didSelectItem:barItem1];
[barItem1 release];
[barItem2 release];
[barItem3 release];
[barItem4 release];
[items release];
- (void)viewDidLoad
{
[super viewDidLoad];
_tabBar.frame = CGRectMake(0,self.view.frame.size.height - 40, self.view.frame.size.width, 40);
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
}
#pragma mark tabBarDelegate
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
_selectedIndex = item.tag;
//清除现在的
UIView* currentView = [self.view viewWithTag:SELECTED_VIEW_CONTROLLER_TAG];
[currentView removeFromSuperview];
//获取最新的
NSDictionary* data = [_arrayViewcontrollers objectAtIndex:item.tag];
UIViewController *viewController = data[@"viewController"];
viewController.view.tag = SELECTED_VIEW_CONTROLLER_TAG;
viewController.view.frame = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height);
viewController.tabBarItem = [[tabBar items] objectAtIndex:item.tag];
[self.view insertSubview:viewController.view belowSubview:_tabBar];
}
-(NSArray *)getViewControllers
{
HomeViewController *viewCtr1 = [[HomeViewController alloc] initWithNibName:nil bundle:nil];
//viewCtr1.title = @"首页";
_ysamgnavigation1 = [[YsmagNavigationViewController alloc] initWithRootViewController:viewCtr1];
SongViewController *viewCtr2 = [[SongViewController alloc] initWithNibName:nil bundle:nil];
//viewCtr2.title = @"歌曲";
_ysamgnavigation2 = [[YsmagNavigationViewController alloc] initWithRootViewController:viewCtr2];
MusckViewController *viewCtr3 = [[MusckViewController alloc] initWithNibName:nil bundle:nil];
//viewCtr3.title = @"音乐";
_ysamgnavigation3 = [[YsmagNavigationViewController alloc] initWithRootViewController:viewCtr3];
SetViewController *viewCtr4 = [[SetViewController alloc] initWithNibName:nil bundle:nil];
//viewCtr4.title = @"设置";
_ysamgnavigation4 = [[YsmagNavigationViewController alloc] initWithRootViewController:viewCtr4];
NSArray *tabBarItems = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:_ysamgnavigation1, @"viewController", nil],
[NSDictionary dictionaryWithObjectsAndKeys:_ysamgnavigation2, @"viewController", nil],
[NSDictionary dictionaryWithObjectsAndKeys:_ysamgnavigation3, @"viewController", nil],
[NSDictionary dictionaryWithObjectsAndKeys:_ysamgnavigation4, @"viewController", nil],nil];
return tabBarItems;
}