IOS学习(十六)自定义tabBar



#import "CustomTabBarItem.h"

@implementation CustomTabBarItem

+ (instancetype) itemWithFrame:(CGRect)frame title:(NSString *)title{
    CustomTabBarItem *item = [CustomTabBarItem buttonWithType:UIButtonTypeCustom];
    [item setFrame:frame];
    
    [item setTitle:title forState:UIControlStateNormal];
    [item.titleLabel setFont:[UIFont systemFontOfSize:17]];
    [item setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [item setBackgroundImage:[UIImage imageNamed:@"n"] forState:UIControlStateNormal];
    [item setBackgroundImage:[UIImage imageNamed:@"s"] forState:UIControlStateSelected];
    return item;
}
@end


#import <UIKit/UIKit.h>

@class CustomTabBar;
//自定义协议,实现与其他类通信
@protocol CustomTabBarDelegate <NSObject>

- (void) tabBar:(CustomTabBar *)tabBar itemSelectedAtIndex:(NSInteger)index;

@end

@interface CustomTabBar : UIView

@property(nonatomic, weak) id<CustomTabBarDelegate> delegate;

+ (instancetype) tabBarWithTites:(NSArray *)titles;
@end

#import "CustomTabBar.h"
#import "CustomTabBarItem.h"

static const CGFloat TabBarHeight = 44;
@interface CustomTabBar()

@property(nonatomic, strong) NSArray *titles;
@property(nonatomic, weak) CustomTabBarItem *preSelectedItem;


@end

@implementation CustomTabBar

+ (instancetype) tabBarWithTites:(NSArray *)titles{
    
    CustomTabBar *tabBar = [[CustomTabBar alloc] init];
    
    [tabBar setTitles:titles];
    [tabBar setBackgroundColor:[UIColor grayColor]];
    
    
    
    return tabBar;
}

- (void)willMoveToSuperview:(UIView *)newSuperview{
    self.frame = CGRectMake(0, CGRectGetMaxY(newSuperview.frame) - TabBarHeight, CGRectGetWidth(newSuperview.frame), TabBarHeight);
    
    //必须是在这里创建items,不然无法获取其frame信息
    [self addItems];
}

- (void) addItems{
    CGFloat count = self.titles.count;
    CGFloat height = CGRectGetHeight(self.bounds);
    CGFloat width = CGRectGetWidth(self.bounds) / count;
    
    for (NSInteger i = 0 ; i < count ; i++){
        CustomTabBarItem *item = [CustomTabBarItem itemWithFrame:CGRectMake(i * width, 0, width, height) title:self.titles[i]];
        [item setTag:i];
        
        [self addSubview:item];
        
        //设置监听,点击按钮时
        [item addTarget:self action:@selector(itemSelected:) forControlEvents:UIControlEventTouchUpInside];
        
    }
}

- (void)itemSelected:(CustomTabBarItem *)send{
    [self.preSelectedItem setSelected:NO];
    [send setSelected:YES];
    
    [self setPreSelectedItem:send];
    
    [self.delegate tabBar:self itemSelectedAtIndex:send.tag];
}

@end

#import "MainViewController.h"
#import "CustomTabBar.h"

@interface MainViewController ()<CustomTabBarDelegate>
@property(nonatomic, strong) UILabel *label;
@property(nonatomic, strong) NSArray *titles;
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.titles = @[@"OC", @"JAVA", @"PHP"];
    
    CustomTabBar *tabBar = [CustomTabBar tabBarWithTites:self.titles];
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(60, 100, 200, 200)];
    [self.label setBackgroundColor:[UIColor grayColor]];
    [self.label setFont:[UIFont systemFontOfSize:60]];
    [self.label setTextColor:[UIColor yellowColor]];
    [self.label setTextAlignment:NSTextAlignmentCenter];
    [self.label setText:@"OC"];
    
    [self.view addSubview:tabBar];
    [self.view addSubview:self.label];
    
    [tabBar setDelegate:self];
    
}

- (void)tabBar:(CustomTabBar *)tabBar itemSelectedAtIndex:(NSInteger)index{
    [self.label setText:self.titles[index]];
}


@end




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值