口袋优选

KBFourButtonView.m 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // KBFourButtonView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/4/28.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBFourButtonView.h"
  9. @implementation KBFourButtonView
  10. - (instancetype)initWithFrame:(CGRect)frame {
  11. self = [super initWithFrame:frame];
  12. if (self) {
  13. self.backgroundColor = [UIColor whiteColor];
  14. self.layer.cornerRadius = 6;
  15. self.layer.masksToBounds = YES;
  16. [self configButtons];
  17. }
  18. return self;
  19. }
  20. - (void)configButtons {
  21. NSArray *titles = @[@"我的订单",@"已领优惠券",@"淘宝购物车",@"浏览记录"];//,@"collection"
  22. NSArray *images = @[@"my_order",@"my_ticket",@"my_car",@"my_browser"];
  23. CGFloat width = self.width/titles.count;
  24. CGFloat height = width;
  25. for (int i = 0; i < titles.count; i++) {
  26. UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(width * i, 0, width, height)];
  27. [button setTitle:titles[i] forState:UIControlStateNormal];
  28. [button setImage:[UIImage imageNamed:images[i]]forState:UIControlStateNormal];
  29. [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
  30. [button setTitleColor:[UIColor YHColorWithHex:0x7c7c7c] forState:UIControlStateNormal];
  31. button.titleLabel.font = [UIFont systemFontOfSize:12];
  32. button.tag = 10000 + i;
  33. // button标题的偏移量
  34. button.titleEdgeInsets = UIEdgeInsetsMake(button.imageView.frame.size.height+5, -button.imageView.bounds.size.width, 0,0);
  35. // button图片的偏移量
  36. button.imageEdgeInsets = UIEdgeInsetsMake(0, button.titleLabel.frame.size.width/2, button.titleLabel.frame.size.height+5, -button.titleLabel.frame.size.width/2);
  37. [self addSubview:button];
  38. }
  39. }
  40. - (void)buttonAction:(UIButton *)sender {
  41. NSInteger index = sender.tag - 10000;
  42. if (self.delegate && [self.delegate respondsToSelector:@selector(YHMineFourButtonViewDidClickButtonIndex:)]) {
  43. [self.delegate YHMineFourButtonViewDidClickButtonIndex:index];
  44. }
  45. }
  46. @end