自定义标签控制器

自定义标签控制器的基类用其他标签控制器来继承于他

MyTabbarController

#define TabBarButtonNumber 5
#define TabBarButtonHight 64
MyTabbarController.h
#import 

@interface MyTabbarController : UITabBarController

@end
MyTabbarController.m
@interface MyTabbarController ()
@property (nonatomic, strong) UIView *myTabBar;
@property (nonatomic, retain) NSArray *normalImageNameArray;
@property (nonatomic, retain) NSArray *selectedImageNameArray;
@end

@implementation MyTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *normalImageNameArray = @[@"1000-0.png", @"1001-0.png", @"1002-0.png", @"1003-0.png", @"1004-0.png"];
    NSArray *selectedImageNameArray = @[@"1000-1.png", @"1001-1.png", @"1002-1.png", @"1003-1.png", @"1004-1.png"];
    self.normalImageNameArray = normalImageNameArray;
    self.selectedImageNameArray = selectedImageNameArray;
    self.tabBar.hidden = YES;
    [self.view addSubview:self.myTabBar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark -- Getter
-(UIView *)myTabBar{
//宏定义按钮的数量与高度
    CGFloat X = TabBarButtonNumber;
    CGFloat btnHight = TabBarButtonHight;

    if (!_myTabBar) {
        _myTabBar = [[UIView alloc]initWithFrame:CGRectMake(0, KScreen_H-64, KScreen_W, 64)];
        for (int i = 0; iUIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake((KScreen_W/X)*(i), 0, KScreen_W/X, btnHight);
            NSString *normalImageName = self.normalImageNameArray[i];
            NSString *selectedImageName = self.selectedImageNameArray[i];
            [btn setImage:IMAGE(normalImageName) forState:UIControlStateNormal];
            [btn setImage:IMAGE(selectedImageName) forState:UIControlStateSelected];
            [_myTabBar addSubview:btn];
            if (btn.selected ==YES) {
                [btn setImage:IMAGE(normalImageName) forState:UIControlStateNormal];
                [btn setImage:IMAGE(normalImageName) forState:UIControlStateHighlighted];
            }else{
                [btn setImage:IMAGE(selectedImageName) forState:UIControlStateHighlighted];
                [btn setImage:IMAGE(selectedImageName) forState:UIControlStateHighlighted|UIControlStateSelected];
            }

            [btn addTarget:self action:@selector(action_Btn:) forControlEvents:UIControlEventTouchDown];
            btn.tag = i+1000;
        }
    }
    return _myTabBar;
}
    //让btn和tabBar联系起来
-(void)action_Btn:(UIButton *)sender{

    self.selectedIndex = sender.tag - 1000;

}

#pragma mark -- Setter

    // 遍历改变btn状态
-(void)setSelectedIndex:(NSUInteger)selectedIndex{
    [super setSelectedIndex:selectedIndex];
    for (int i = 1000; i<1005; ++i) {
        UIButton *btn = [self.myTabBar viewWithTag:i];
        if (i == selectedIndex+1000) {
            btn.selected = YES;
        }else{
            btn.selected = NO;
        }
    }
}
子类控制器方法
- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray *tabbarviewvcs = [NSMutableArray arrayWithCapacity:TabBarButtonNumber];
    RecordViewController *recordvc1 = [[RecordViewController alloc]init];
    [tabbarviewvcs addObject:recordvc1];
    AccessViewController *recordvc2 = [[AccessViewController alloc]init];
    [tabbarviewvcs addObject:recordvc2];
    MedicineViewController *recordvc3 = [[MedicineViewController alloc]init];
    [tabbarviewvcs addObject:recordvc3];
    KnowlageViewController *recordvc4 = [[KnowlageViewController alloc]init];

    [tabbarviewvcs addObject:recordvc4];
    seedoctorViewController *recordvc5 = [[seedoctorViewController alloc]init];
    [tabbarviewvcs addObject:recordvc5];
    self.viewControllers = tabbarviewvcs;
}

你可能感兴趣的:(ios开发)