iOS自定义tabbar

使用方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSArray *classNameArray = @[@"HomeViewController",@"SecondViewController",@"ThirdViewController",@"ForthViewController",@"FifthViewController"];
    NSMutableArray *subViewControllers = [NSMutableArray array];
    for (NSString *title in TITLEARRAY) {
        NSInteger index = [TITLEARRAY indexOfObject:title];
        UIViewController *vc = [[NSClassFromString(classNameArray[index]) alloc] init];
        vc.title = title;
        LYNavigationController *nav = [[LYNavigationController alloc]initWithRootViewController:vc];
        nav.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -4);
        [subViewControllers addObject:nav];
    }
    LYTabBarController *tabVC = [[LYTabBarController alloc]initWithViewControllers:subViewControllers];
    self.window.rootViewController = tabVC;
    return YES;
}

实现代码

#define TITLEARRAY @[@"首页",@"成绩关注",@"家长中心",@"家长社区",@"关于我们"]

#import <UIKit/UIKit.h>
#import "LYTabBar.h"
@interface LYTabBarController : UITabBarController<LYTabBarDelegate>

- (instancetype)initWithViewControllers:(NSArray *)controllers;

@end
#import "LYTabBarController.h"

@interface LYTabBarController () <LYTabBarDelegate>
@property (nonatomic, weak) UIButton *selectedBtn;
@end

@implementation LYTabBarController

- (instancetype)initWithViewControllers:(NSArray *)controllers{
    if (self = [super init]) {
        self.viewControllers = controllers;
        CGRect rect = self.tabBar.bounds;
        LYTabBar *myView = [[LYTabBar alloc] init];
        myView.delegate = self;
        myView.frame = rect;
        [self.tabBar addSubview:myView];
        for (int i=0; i<self.viewControllers.count; i++) {
            NSString *imageName = [NSString stringWithFormat:@"tabbar_n%d", i];
            NSString *imageNameSel = [NSString stringWithFormat:@"tabbar_h%d", i];
            UIImage *image = [UIImage imageNamed:imageName];
            UIImage *imageSel = [UIImage imageNamed:imageNameSel];
            [myView addButtonWithImage:image selectedImage:imageSel andTitle:[TITLEARRAY objectAtIndex:i]];
        }
        self.tabBar.barTintColor = [UIColor colorWithRed:38/255.0 green:38/255.0 blue:38/255.0 alpha:1];
        self.tabBar.selectedImageTintColor = [UIColor whiteColor];
    }
    return self;
}

- (void)tabBar:(LYTabBar *)tabBar selectedFrom:(NSInteger)from to:(NSInteger)to {
    self.selectedIndex = to;
}

@end
#import <UIKit/UIKit.h>

@class LYTabBar;

@protocol LYTabBarDelegate <NSObject>

- (void)tabBar:(LYTabBar *)tabBar selectedFrom:(NSInteger) from to:(NSInteger)to;

@end

@interface LYTabBar : UIView
@property(nonatomic,weak) id<LYTabBarDelegate> delegate;
-(void)addButtonWithImage:(UIImage *)image selectedImage:(UIImage *) selectedImage andTitle:(NSString *)title;
@end

@interface LYTabBarButton : UIButton

@end
#import "LYTabBar.h"
@interface LYTabBar ()

@property (nonatomic, weak) UIButton *selectedBtn;
@end

@implementation LYTabBar

- (void)addButtonWithImage:(UIImage *)image selectedImage:(UIImage *)selectedImage andTitle:(NSString *)title{
    LYTabBarButton *btn = [[LYTabBarButton alloc] init];
    [btn setImage:image forState:UIControlStateNormal];
    [btn setImage:selectedImage forState:UIControlStateSelected];
    [self addSubview:btn];
    [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    if (self.subviews.count == 1) {
        [self clickBtn:btn];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    NSInteger count = self.subviews.count;
    for (int i = 0; i < count; i++) {
        UIButton *btn = self.subviews[i];
        btn.tag = i;
        CGFloat x = i * self.bounds.size.width / count;
        CGFloat y = 0;
        CGFloat width = self.bounds.size.width / count;
        CGFloat height = self.bounds.size.height;
        btn.frame = CGRectMake(x, y, width, height);
    }
}

- (void)clickBtn:(UIButton *)button {
    self.selectedBtn.selected = NO;
    button.selected = YES;
    self.selectedBtn = button;
    if ([self.delegate respondsToSelector:@selector(tabBar:selectedFrom:to:)]) {
        [self.delegate tabBar:self selectedFrom:self.selectedBtn.tag to:button.tag];
    }
}

@end

@implementation LYTabBarButton

- (CGRect)imageRectForContentRect:(CGRect)contentRect{
    return  CGRectMake(24 , 10 , 17 , 17);
}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值