自定義UITabbarController

//.h
#import <UIKit/UIKit.h>
#define ScrollWidht self.view.bounds.size.width
#define ScrollHight self.view.bounds.size.height
#define CustomTabBarHight 49
#define ImageWidth 30
#define ImageHight 30
#define ImageNumber 4
@interface MyTabBarViewController : UITabBarController

@property (assign,readwrite,nonatomic)UIView * selectionIndicatorImage;
@end
//.m
#import "MyTabBarViewController.h"
#import "FristViewController.h"
#import "SecondViewController.h"
#import "ThridViewController.h"
#import "FouthViewController.h"
@interface MyTabBarViewController ()
{
    UIButton * resetButton;
}
@end

@implementation MyTabBarViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self createViewControlls];
    
    [self createTabBar];
}

- (UINavigationController *)createNavigationControllerWithVCClassName:(NSString * )className
{
    Class vcClass = NSClassFromString(className);
    
    UIViewController * vcClassName = [[vcClass alloc]init];
    
    return [[UINavigationController alloc]initWithRootViewController:vcClassName];
}

- (void)createViewControlls
{
    NSArray * arrayOfClass = @[@"FristViewController",@"SecondViewController",@"ThridViewController",@"FouthViewController"];
    
    NSMutableArray * vcArray = [[NSMutableArray alloc]init];
    
    for (int i = 0; i < arrayOfClass.count; i++)
    {
        UINavigationController * nav = [self createNavigationControllerWithVCClassName:arrayOfClass[i]];
        
        [vcArray addObject:nav];
    }
    
    self.viewControllers = vcArray;
}

- (void)createTabBar
{
    self.tabBar.hidden = YES;
    
    UIImageView  * customtabBarView = [[UIImageView alloc]init];
    
    //customtabBarView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8];
    
    customtabBarView.backgroundColor = [UIColor whiteColor];
    
    customtabBarView.alpha = 0.8;
    
    customtabBarView.userInteractionEnabled = YES;
    
    float customtabBarViewX = 0;
    float customtabBarViewY = ScrollHight - CustomTabBarHight;
    
    customtabBarView.frame = CGRectMake(customtabBarViewX, customtabBarViewY, ScrollWidht, CustomTabBarHight);
    
    [self.view addSubview:customtabBarView];
    
    float btnSpace = (ScrollWidht - ImageWidth*ImageNumber)/5.0;
    
    float btnY = (CustomTabBarHight - ImageHight)/2.0;
    
    for (int i = 0; i < ImageNumber; i++)
    {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
        
        float btnX = btnSpace + (btnSpace + ImageWidth)*i;
        
        btn.frame = CGRectMake(btnX, btnY, ImageWidth, ImageHight);
        
        btn.tag = 100+i;
        
        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_%d.png",i]] forState:UIControlStateNormal];
        
        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_c%d.png",i]] forState:UIControlStateSelected];
        
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i == 0)
        {
            btn.selected = YES;
            resetButton = btn;
            
            _selectionIndicatorImage = [self setSelectedImage];
            
            _selectionIndicatorImage.frame = btn.frame;
            
            [customtabBarView addSubview:_selectionIndicatorImage];
        }
        
        [customtabBarView addSubview:btn];
    }
}

- (UIView *)setSelectedImage
{
    static  UIView * view = nil;
    if (!view)
    {
        view  = [[UIView alloc]init];
        view.backgroundColor = [UIColor orangeColor];
    }
   
    return view;
}

- (void)btnClick:(UIButton *)btn
{
    resetButton.selected = NO;
    resetButton = btn;
    btn.selected = YES;
    
    CGPoint centerOfselectionIndicatorImage = btn.center;
    
    [UIView animateWithDuration:0.25 animations:^{
       
        _selectionIndicatorImage.center = centerOfselectionIndicatorImage;
        
    }];
    
    self.selectedIndex = btn.tag - 100;
}

@end

總結:使用預定義使得代碼具有可擴展性,編程過程中.m文件儘量避免出現數字,命名一定要規範,條理要清晰!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章