大前端-BFE程序员ReactNative系列

iOS自定义的Tab

2019-08-18  本文已影响8人  码工人生

网上搜索ios tab会有很多例子。本文我写一篇,自定义的tab,主要和其他不同:1.文本会适应;2.右上角有可定制的提示框。


屏幕快照 2019-08-18 15.48.18.png

提供很多定制属性:

/**
 水平方向两边的间距 默认为0
 */
@property (nonatomic,assign) float horizontalMargin;

/**
 顶部的间距 默认为0
 */
@property (nonatomic,assign) float topMargin;
/**
 底部的间距 默认为0
 */
@property (nonatomic,assign) float bottomMargin;

/**
 文案的数组
 */
@property (nonatomic,strong) NSArray *titles;


/**
 指定选中位置  默认为 0
 */
@property (nonatomic,assign) NSInteger selectIndex;

/**
 *  单个item背景色,默认白色
 */
@property (nonatomic,strong)UIColor *itemBackColor;

/**
 字体字号 默认14号
 */
@property (nonatomic,assign) float fontSize;

/**
 *  底部移动线条颜色
 */
@property (nonatomic,strong)UIColor *lineColor;

/**
 横线相对文字长度内缩举例 默认线为20
 */
@property (nonatomic,assign) float linePadding;

/**
 *  默认字体颜色 
 */
@property (nonatomic,strong)UIColor *itemColor;

/**
 *  选中颜色
 */
@property (nonatomic,strong)UIColor *itemSelctedColor;

/**
 选中位置后回调
 */
@property (nonatomic,copy) IndexChangeBlock indexChangeBlock;

/**
 显示右上角图标提示

 @param index 第几个tab,默认从0开始
 @param title 显示的文案
 @param badgeStyleDict 图标样式 非必传
 {
 @"backColor":@"#ffffff",默认橘黄色
 @"textColor":@"#ccccc",默认白色
 @"textSize":@(10) 默认10号
 }
 @
 */
- (void)showBadgeAtIndex:(NSInteger)index title:(NSString *)title badgeStyle:(NSDictionary *)badgeStyleDict;

调用方法也十分简单

    self.tabBarView = [[ZHXTabBarView alloc]initWithFrame:CGRectMake(0, 170, screen_width, 50) titles:@[@"男装",@"女装",@"儿童服装"]];
    [self.view addSubview:self.tabBarView];
    self.tabBarView.horizontalMargin = 20;
    self.tabBarView.fontSize = 15;
    self.tabBarView.lineColor = [UIColor blueColor];
    self.tabBarView.linePadding = 5;
    self.tabBarView.itemColor = [UIColor blackColor];
    self.tabBarView.itemSelctedColor = [UIColor blueColor];
    [self.tabBarView showBadgeAtIndex:1 title:@"减80" badgeStyle:@{@"backColor":@"#cccccc",@"textColor":@"#ffffff"}];

    [self.tabBarView setIndexChangeBlock:^(NSInteger index) {
        NSLog(@"%ld位置",index);
    }];
    
    
    self.tabBarTwoView = [[ZHXTabBarView alloc]initWithFrame:CGRectMake(0, 300, screen_width, 50) titles:@[@"服务家电",@"电子商品"]];
    [self.view addSubview:self.tabBarTwoView];
    self.tabBarTwoView.backgroundColor = [UIColor grayColor];
    self.tabBarTwoView.horizontalMargin = 0;
    self.tabBarTwoView.fontSize = 15;
    self.tabBarTwoView.lineColor = [UIColor redColor];
    self.tabBarTwoView.linePadding = 5;
    self.tabBarTwoView.itemColor = [UIColor blackColor];
    self.tabBarTwoView.itemSelctedColor = [UIColor redColor];
    [self.tabBarTwoView showBadgeAtIndex:0 title:@"大促销" badgeStyle:@{@"backColor":@"#cccccc",@"textColor":@"#ffffff"}];
    
    [self.tabBarTwoView setIndexChangeBlock:^(NSInteger index) {
        NSLog(@"%ld位置",index);
    }];
    
    self.tabBarThreeView = [[ZHXTabBarView alloc]initWithFrame:CGRectMake(0, 400, screen_width, 50) titles:@[@"机票",@"酒店",@"汽车/船票",@"火车票"]];
    [self.view addSubview:self.tabBarThreeView];
    self.tabBarThreeView.backgroundColor = [UIColor lightGrayColor];
    self.tabBarThreeView.horizontalMargin = 0;
    self.tabBarThreeView.fontSize = 15;
    self.tabBarThreeView.lineColor = [UIColor cyanColor];
    self.tabBarThreeView.linePadding = 5;
    self.tabBarThreeView.selectIndex = 2;
    self.tabBarThreeView.itemColor = [UIColor blackColor];
    self.tabBarThreeView.itemSelctedColor = [UIColor cyanColor];
    
    [self.tabBarThreeView setIndexChangeBlock:^(NSInteger index) {
        NSLog(@"%ld位置",index);
    }];

Demo链接

上一篇下一篇

猜你喜欢

热点阅读