DJStatusToolBar.m
#import "DJStatusToolBar.h" @interface DJStatusToolBar() // 存放所有按钮 @property (nonatomic,strong) NSMutableArray *btns; // 存放所有分割线 @property (nonatomic,strong) NSMutableArray *dividers; @end @implementation DJStatusToolBar - (NSMutableArray *)btns { if (!_btns) { _btns = [NSMutableArray array]; } return _btns; } - (NSMutableArray *)dividers { if (!_dividers) { _dividers = [NSMutableArray array]; } return _dividers; } + (instancetype)toolbar { return [[self alloc] init]; } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { /* 添加转发等按钮 */ [self setupBtn:@"转发" image:@"timeline_icon_retweet"]; [self setupBtn:@"评论" image:@"timeline_icon_comment"]; [self setupBtn:@"赞" image:@"timeline_icon_unlike"]; /* 添加分割线 */ [self setupDivider]; [self setupDivider]; } return self; } // 添加按钮 - (void)setupBtn:(NSString *)title image:(NSString *)imageName { UIButton *btn = [[UIButton alloc] init]; [btn setTitle:title forState:UIControlStateNormal]; [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; btn.titleLabel.font = [UIFont systemFontOfSize:14]; [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 8, 0, 0)]; [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal]; [self addSubview:btn]; [self.btns addObject:btn]; } - (void)setupDivider { UIImageView *divider = [[UIImageView alloc] init]; divider.image = [UIImage imageNamed:@"timeline_card_bottom_line"]; [self addSubview:divider]; [self.dividers addObject:divider]; } - (void)layoutSubviews { [super layoutSubviews]; /* 设置按钮的frame */ NSUInteger btnCount = self.btns.count; CGFloat btnW = self.width / btnCount; CGFloat btnH = self.height; for (int i = 0; i < btnCount; i++) { UIButton *btn = self.btns[i]; btn.x = i * btnW; btn.y = 0; btn.width = btnW; btn.height = btnH; } /* 设置分割线的frame */ NSUInteger dividerCount = self.dividers.count; for (int i = 0; i < dividerCount; i++) { UIImageView *divider = self.dividers[i]; divider.width = 1; divider.height = btnH; divider.x = (i + 1) * btnW; divider.y = 0; } } @end
最终效果:
时间: 2024-10-17 12:23:52