美文网首页
自定义TabBar的样式

自定义TabBar的样式

作者: Bonucci | 来源:发表于2017-03-14 10:16 被阅读0次

    如果无意间进来发现有用帮着点个喜欢!

    #define imageNamed(str) [UIImage imageNamed:[NSString stringWithFormat:@"skin_needed.bundle/image/%@",str]]
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [self clearTabBarTopLine];
        
        [self initViewController];
    }
    
    - (void) initViewController {
        HomeViewController *home=[[HomeViewController alloc]init];
        NavigationViewController *navHome = [[NavigationViewController alloc] initWithRootViewController:home];
        navHome.tabBarItem.image = [imageNamed(@"homeA") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        navHome.tabBarItem.selectedImage = [imageNamed(@"homeL") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        navHome.tabBarItem.title = @"保障";
        
        
        FamilyViewController *family=[[FamilyViewController alloc]init];
        NavigationViewController *navFamily = [[NavigationViewController alloc] initWithRootViewController:family];
        navFamily.tabBarItem.image = [imageNamed(@"reportA") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        navFamily.tabBarItem.selectedImage = [imageNamed(@"reportL") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        navFamily.tabBarItem.title = @"家庭";
    
        
        DisCoverViewController *view=[[DisCoverViewController alloc]init];
        NavigationViewController *navView = [[NavigationViewController alloc] initWithRootViewController:view];
        navView.tabBarItem.image = [imageNamed(@"baseAct") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        navView.tabBarItem.selectedImage = [imageNamed(@"baseAct") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        
        DisCoverViewController *discover=[[DisCoverViewController alloc]init];
        NavigationViewController *navDiscover = [[NavigationViewController alloc] initWithRootViewController:discover];
        navDiscover.tabBarItem.image = [imageNamed(@"messageA") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        navDiscover.tabBarItem.selectedImage = [imageNamed(@"messageL") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        navDiscover.tabBarItem.title = @"发现";
        
        
        MineViewController *mine=[[MineViewController alloc]init];
        NavigationViewController *navMine = [[NavigationViewController alloc] initWithRootViewController:mine];
        navMine.tabBarItem.image = [imageNamed(@"shopA") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        navMine.tabBarItem.selectedImage = [imageNamed(@"shopL") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        navMine.tabBarItem.title = @"我";
        
        self.viewControllers =  @[navHome,navFamily,navView,navDiscover,navMine];
        
        
        //未选中
        [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor lightGrayColor],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
        
    //    选中
        [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:COLOR_UI_54C1F5,NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    }
    
    - (void) viewWillLayoutSubviews
    {
        [super viewWillLayoutSubviews];
        
        //重新设置tabbar 的高度
        CGRect frame = self.tabBar.frame;
        frame.size.height = TABBAR_HEIGHT;
        frame.origin.y = self.view.frame.size.height - TABBAR_HEIGHT;
        self.tabBar.frame = frame;
        self.tabBar.barStyle = UIBarStyleDefault;
        [self.tabBar setBackgroundImage:imageNamed(@"bgbgbg")];
        //因为加高tabbar ,重新设置图片偏移
        for (int i = 0; i<self.tabBar.items.count; i++) {
            UITabBarItem *item = self.tabBar.items[i];
            if (i != 2) {
                item.imageInsets = UIEdgeInsetsMake(TABBAR_HEIGHT - 49 - 5, 0, -(TABBAR_HEIGHT - 49 - 5), 0);
            } else {
                item.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
            }
        }
    }
    
    - (void) clearTabBarTopLine
    {
        CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        
        UIGraphicsBeginImageContext(rect.size);
        
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
        
        CGContextFillRect(context, rect);
        
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        [self.tabBar setBackgroundImage:img];
        [self.tabBar setShadowImage:img];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    

    相关文章

      网友评论

          本文标题:自定义TabBar的样式

          本文链接:https://www.haomeiwen.com/subject/owpcnttx.html