// // KBFourButtonView.m // YouHuiProject // // Created by 小花 on 2018/4/28. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBFourButtonView.h" @implementation KBFourButtonView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor whiteColor]; self.layer.cornerRadius = 6; self.layer.masksToBounds = YES; [self configButtons]; } return self; } - (void)configButtons { NSArray *titles = @[@"我的订单",@"已领优惠券",@"淘宝购物车",@"浏览记录"];//,@"collection" NSArray *images = @[@"my_order",@"my_ticket",@"my_car",@"my_browser"]; CGFloat width = self.width/titles.count; CGFloat height = width; for (int i = 0; i < titles.count; i++) { UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(width * i, 0, width, height)]; [button setTitle:titles[i] forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:images[i]]forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [button setTitleColor:[UIColor YHColorWithHex:0x7c7c7c] forState:UIControlStateNormal]; button.titleLabel.font = [UIFont systemFontOfSize:12]; button.tag = 10000 + i; // button标题的偏移量 button.titleEdgeInsets = UIEdgeInsetsMake(button.imageView.frame.size.height+5, -button.imageView.bounds.size.width, 0,0); // button图片的偏移量 button.imageEdgeInsets = UIEdgeInsetsMake(0, button.titleLabel.frame.size.width/2, button.titleLabel.frame.size.height+5, -button.titleLabel.frame.size.width/2); [self addSubview:button]; } } - (void)buttonAction:(UIButton *)sender { NSInteger index = sender.tag - 10000; if (self.delegate && [self.delegate respondsToSelector:@selector(YHMineFourButtonViewDidClickButtonIndex:)]) { [self.delegate YHMineFourButtonViewDidClickButtonIndex:index]; } } @end