UITabBarController自定义(一)

废话不多说,直接代码了:

.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;

    

}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值