iOS7 Custom UITabBarController

来源:互联网 发布:淘白菜淘宝客 编辑:程序博客网 时间:2024/06/17 07:06

简单总结一下UITabBarController的使用

1.设置tabBar的背景图片和选中的指示图片

    FirstViewController *firstCtl = [[FirstViewController alloc] init];    SecondViewController *secCtl = [[SecondViewController alloc] init];    ThirdViewController *thirdCtl = [[ThirdViewController alloc] init];    FourthViewController *fourthCtl = [[FourthViewController alloc] init];        firstCtl.view.backgroundColor = [UIColor whiteColor];    secCtl.view.backgroundColor = [UIColor redColor];    thirdCtl.view.backgroundColor = [UIColor greenColor];    fourthCtl.view.backgroundColor = [UIColor blueColor];        UINavigationController *firstNav = [[UINavigationController alloc] initWithRootViewController:firstCtl];    UINavigationController *secNav = [[UINavigationController alloc] initWithRootViewController:secCtl];    UINavigationController *thrNav = [[UINavigationController alloc] initWithRootViewController:thirdCtl];    UINavigationController *fouNav = [[UINavigationController alloc] initWithRootViewController:fourthCtl];        self.tabBarCtl = [[UITabBarController alloc] init];    _tabBarCtl.viewControllers = @[firstNav, secNav, thrNav, fouNav];        //更改tabBar的frame, 原高度49,变为60    CGRect rect = _tabBarCtl.tabBar.frame;    rect.size.height += 11;    rect.origin.y -= 11;    _tabBarCtl.tabBar.frame = rect;            //设置tabBar 的背景图片    [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"backgroud.png"]];    //设置tabBar的选中的背景图片    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"dback.png"]];


2.设置tabBarItem的图片和标题

    //tabBarItem 设置标题和图片    firstNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"one"                                                                                    image:[UIImage imageNamed:@"1nor.png"]                                                                      selectedImage:[UIImage imageNamed:@"1sel.png"]];    secNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"two"                                                                                    image:[UIImage imageNamed:@"2nor.png"]                                                                      selectedImage:[UIImage imageNamed:@"2sel.png"]];    thrNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"three"                                                                                    image:[UIImage imageNamed:@"3nor.png"]                                                                      selectedImage:[UIImage imageNamed:@"3sel.png"]];    fouNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"four"                                                                                    image:[UIImage imageNamed:@"4nor.png"]                                                                      selectedImage:[UIImage imageNamed:@"4sel.png"]];

3.设置tabBarItem title的颜色 和 字体大小,并调整title 和 图片的位置

    //改变tabBar 上title的颜色 和 字体大小    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:102/255.0 green:122/255.0 blue:143/255.0 alpha:1.0f], NSForegroundColorAttributeName, [UIFont systemFontOfSize:14], NSFontAttributeName, nil] forState:UIControlStateNormal];    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:0/255.0 green:198/255.0 blue:255/255.0 alpha:1.0f], NSForegroundColorAttributeName, [UIFont systemFontOfSize:14], NSFontAttributeName,nil] forState:UIControlStateSelected];        //改变UITabBarItem的title的位置    [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0, -5.0)];        //改变UITabBarItem的图片的位置    firstNav.tabBarItem.imageInsets = UIEdgeInsetsMake(-6, 0, 6, 0);    secNav.tabBarItem.imageInsets = UIEdgeInsetsMake(-6, 0, 6, 0);    thrNav.tabBarItem.imageInsets = UIEdgeInsetsMake(-6, 0, 6, 0);    fouNav.tabBarItem.imageInsets = UIEdgeInsetsMake(-6, 0, 6, 0);


4.push一个页面的时候,使tabBar一起移动,而不是隐藏tabBar

- (void)pushToViewCtl:(UIButton *)button{    PushViewController *pushCtl = [[PushViewController alloc] init];    self.hidesBottomBarWhenPushed = YES;    [self.navigationController pushViewController:pushCtl animated:YES];    self.hidesBottomBarWhenPushed = NO;}


自定义UITab bar还可以参考

iOS Programming 101: How To Customize Tab Bar Background and Appearance

自定义UITabBarItem

有两处可以参考

为你的TabBar添加Badge

enryold/UITabBarItem-CustomBadge



0 0