自定义Tabbar封装

来源:互联网 发布:通达信股票行情软件 编辑:程序博客网 时间:2024/05/21 09:08
// 1. CustomTabbar.h#import <UIKit/UIKit.h>@interface CutomTabBar : UIView@property (nonatomic,strong) NSArray *images;@property (nonatomic,strong) NSArray *seletedImages;@property (nonatomic,strong) NSArray *titles;@property (nonatomic,strong) NSArray *buttons;/** 初始化CutomTabBar对象 */+ (id)tabBarWithImages:(NSArray *)images         seletedImages:(NSArray *)seletedImages                titles:(NSArray *)titles;@end// 2. CustomTabbar.m@interface CutomTabBar ()@end@implementation CutomTabBar+ (id)tabBarWithImages:(NSArray *)images         seletedImages:(NSArray *)seletedImages                titles:(NSArray *)titles {        CutomTabBar *tabBar = [[CutomTabBar alloc] init];    for (int i = 0; i < titles.count; i++) {                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];        [tabBar addSubview:button];                // frame        CGFloat buttonW = 375.0f/titles.count;        CGFloat buttonX = buttonW * i;        CGFloat buttonY = 0;        CGFloat buttonH = 49;        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);                // titles        [button setTitle:titles[i] forState:UIControlStateNormal];        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];        button.titleLabel.font = [UIFont systemFontOfSize:10.0f];        button.titleEdgeInsets = UIEdgeInsetsMake(60, -30, 0, 0);        // images        NSString *imageName  = [images objectAtIndex:i];        NSString *selectedImageName = [seletedImages objectAtIndex:i];        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];        [button setImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateSelected];        button.imageEdgeInsets = UIEdgeInsetsMake(10, 17, 0, 0);        // 事件        [button addTarget:tabBar action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];        button.tag = i;    }        return tabBar;}- (void)drawRect:(CGRect)rect {    UIImage *image = [UIImage imageNamed:@"导航栏"];//    [image drawAtPoint:CGPointZero];//    [image drawAsPatternInRect:rect];    [image drawInRect:rect];}#pragma mark - 按钮点击事件- (void)buttonAction:(UIButton *)button {        for (UIButton *btn in self.subviews) {        btn.selected = NO;    }    NSLog(@"index - %ld", button.tag);}- (void)willMoveToSuperview:(UIView *)newSuperview {        self.frame = CGRectMake(0, 667-64, 375, 64);    self.backgroundColor = [UIColor whiteColor];    }@end

0 0
原创粉丝点击