自定义

//
//  CCTabBarController.h
//  IOSTest
//
//  Created by wangcccong on 13-11-22.
//  Copyright (c) 2013年 MacOS. All rights reserved.
//
 
#import <UIKit/UIKit.h>
 
@interface CCTabBarController : UITabBarController
{
@private
     UIImageView *_tarbarImageView;
     UIImageView *_selectedIndicatroView;
     BOOL _isShow;
}
@property (strong, nonatomic) UIButton *selectedButton;
//初始化TabBarController
//controllers: UIViewController
//normalNames: 每个选项正常时的图片
//selectedNames: 选中时图片
//model : 每个选项的权限(0--隐藏, 1--显示)
- (id)initWithControllers:(NSArray *)controllers tabbarNormalImageNames:(NSArray *)normalNames tabbarSelectedImageNames:(NSArray *)selectedName competence:(NSArray *)model;
 
@end
 
@interface CCTabBarController (TabBar)
//设置背景图片
- (void)setCCTabBarBackgroundImage:(UIImage *)image;
//显示TabBar
- (void)showCCTabBar;
//隐藏TabBar
- (void)hideCCTabBar;
 
@end
 
@interface CCTabBarController (TabBarIndicator)
//设置 动画图片
- (void)setCCSelectedIndicatorImage:(UIImage *)image;
 
@end
 
//
//  CCTabBarController.m
//  IOSTest
//
//  Created by wangcccong on 13-11-22.
//  Copyright (c) 2013年 MacOS. All rights reserved.
//
 
#import "CCTabBarController.h"
#import "UIButton+CCButton.h"
 
@interface CCTabBarController ()
 
- (void)tarbarChanged:(UIButton *)button;
 
@end
 
@implementation CCTabBarController
@synthesize selectedButton = _selectedButton;
 
- (id)initWithControllers:(NSArray *)controllers tabbarNormalImageNames:(NSArray *)normalNames tabbarSelectedImageNames:(NSArray *)selectedName competence:(NSArray *)model
{
     self = [self init];
     if (self) {
 
        //根据权限设置需要的controller,normalImage、selectedImage
 
         __block NSMutableArray *tempNormalImages = [NSMutableArray arrayWithArray:normalNames];
         __block NSMutableArray *tempSelectedImages = [NSMutableArray arrayWithArray:selectedName];
         __block NSMutableArray *tempControllers = [NSMutableArray arrayWithArray:controllers];
         [controllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
             if ([[model objectAtIndex:idx] isEqualToString:@"0"]) {
                 [tempControllers removeObject:obj];
                 [tempNormalImages removeObjectAtIndex:idx];
                 [tempSelectedImages removeObjectAtIndex:idx];
             }
             if (idx == normalNames.count - 1) {
                 *stop = YES;
             }
         }];
         
         CGRect screenRect = [[UIScreen mainScreen] bounds];
         _tarbarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, screenRect.size.height - 49, screenRect.size.width, 49)];
         [_tarbarImageView setImage:[UIImage imageNamed:@"tabbar_bg.jpg"]];
         _tarbarImageView.userInteractionEnabled = YES;
         
         for (int i = 0; i < tempNormalImages.count; i++) {
             UIButton *button = [[UIButton alloc] initWithNormalImage:[UIImage imageNamed:[tempNormalImages objectAtIndex:i]] andSlected:[UIImage imageNamed:[tempSelectedImages objectAtIndex:i]] frame:CGRectMake(i * 64, 0, 64, 49)];
             button.tag = i;
             [button addTarget:self action:@selector(tarbarChanged:) forControlEvents:UIControlEventTouchUpInside];
             [_tarbarImageView addSubview:button];
             if (i == 0) {
                 _selectedButton = button;
             }
         }
         _selectedButton.selected = YES;
         _selectedIndicatroView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 49)];
         [_selectedIndicatroView setImage:[UIImage imageNamed:@"tarbar_indicator.png"]];
         [_tarbarImageView addSubview:_selectedIndicatroView];
         [self.view addSubview:_tarbarImageView];
         _isShow = YES;
         [self setViewControllers:tempControllers animated:YES];
     }
     return self;
}
 
#pragma mark - tarbar
- (void)setCCTabBarBackgroundImage:(UIImage *)image
{
     [_tarbarImageView setImage:image];
}
 
- (void)showCCTabBar
{
     if (!_isShow) {
         [UIView beginAnimations:nil context:NULL];
         [UIView setAnimationDuration:.35f];
         _tarbarImageView.frame = CGRectMake(0, _tarbarImageView.frame.origin.y, _tarbarImageView.frame.size.width, _tarbarImageView.frame.size.height);
         [UIView commitAnimations];
         _isShow = YES;
     }
}
 
- (void)hideCCTabBar
{
     if (_isShow) {
         [UIView beginAnimations:nil context:NULL];
         [UIView setAnimationDuration:.35f];
         _tarbarImageView.frame = CGRectMake(- _tarbarImageView.frame.size.width, _tarbarImageView.frame.origin.y, _tarbarImageView.frame.size.width, _tarbarImageView.frame.size.height);
         [UIView commitAnimations];
         _isShow = NO;
     }
}
 
#pragma mark - tarbar indicator
- (void)setCCSelectedIndicatorImage:(UIImage *)image
{
     [_selectedIndicatroView setImage:image];
}
 
#pragma mark - private
- (void)tarbarChanged:(UIButton *)button
{
     if (self.selectedIndex != button.tag) {
         _selectedButton.selected = NO;
         _selectedButton = button;
         [UIView animateWithDuration:.1f animations:^{
             _selectedIndicatroView.frame = CGRectMake(_selectedButton.tag * 64, 0, 64, 49);
         } completion:^(BOOL finished) {
             self.selectedIndex = button.tag;
             _selectedButton.selected = YES;
         }];
     }
}
 
@end
 
 
//具体使用
 
//添加UIViewController,在这里使用了UINavigationController
NetworkUnitController *networkUnit = [[NetworkUnitController alloc] init];
     UINavigationController *navNetworkUnit = [[UINavigationController alloc]initWithRootViewController:networkUnit];
     if ([navNetworkUnit.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
         [navNetworkUnit.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar_bg"] forBarMetrics:UIBarMetricsDefault];
     }
     
     FireDisastorController *fireDisastor = [[FireDisastorController alloc] init];
     UINavigationController *navFireDisastor = [[UINavigationController alloc] initWithRootViewController:fireDisastor];
     if ([navFireDisastor.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
         [navFireDisastor.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar_bg"] forBarMetrics:UIBarMetricsDefault];
     }
     
     ModifyApplicationController *modifyApplication = [[ModifyApplicationController alloc] init];
     UINavigationController *navModifyApplication = [[UINavigationController alloc] initWithRootViewController:modifyApplication];
     if ([navModifyApplication.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
         [navModifyApplication.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar_bg"] forBarMetrics:UIBarMetricsDefault];
     }
    
     //选项图片,包括选中时和为选中的图片
     NSArray *controllers = @[navNetworkUnit, navFireDisastor, navModifyApplication];
     NSArray *normalImages = @[@"area_networkunit_normal.jpg", @"area_firedisastor_normal.jpg", @"area_modifyapplication_normal.png"];
     NSArray *selectedImages = @[@"area_networkunit_selected.jpg", @"area_firedisastor_selected.jpg", @"area_modifyapplication_selected.png"];
     //权限,如果你的项目中不使用到权限问题,可以省略
     NSArray *model = @[@"1", @"1", @"1"];
     //初始化
     CCTabBarController *tabBarController = [[CCTabBarController alloc] initWithControllers:controllers tabbarNormalImageNames:normalImages tabbarSelectedImageNames:selectedImages competence:model];
     if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]) {
         [self presentViewController:tabBarController animated:NO completion:^{
             
         }];
     }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值