HJMeDistributeView.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // HJMeDistributeView.m
  3. // HappyJob
  4. //
  5. // Created by Bob on 2019/4/18.
  6. // Copyright © 2019 Bob. All rights reserved.
  7. //
  8. #import "HJMeDistributeView.h"
  9. #import "UIButton+Layout.h"
  10. @interface HJMeDistributeView ()
  11. @end
  12. @implementation HJMeDistributeView
  13. - (instancetype)init {
  14. if (self = [super init])
  15. {
  16. [self addSubview:self.favoriteButton];
  17. [self addSubview:self.applyButton];
  18. [self addSubview:self.invitationButton];
  19. [self makeConstraints];
  20. }
  21. return self;
  22. }
  23. #pragma mark - life cycle
  24. - (void)makeConstraints {
  25. // 三等分布局
  26. CGSize buttonSize = self.favoriteButton.frame.size;
  27. NSArray *views = @[self.favoriteButton, self.applyButton, self.invitationButton];
  28. CGFloat leadSpacing = 40;
  29. CGFloat tailSpacing = 40;
  30. CGFloat fixedSpacing = (SCREEN_WIDTH - buttonSize.width * views.count - leadSpacing - tailSpacing) / (views.count - 1);
  31. [views mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:fixedSpacing leadSpacing:leadSpacing tailSpacing:tailSpacing];
  32. [views mas_makeConstraints:^(MASConstraintMaker *make) {
  33. make.top.equalTo(self).with.offset(0);
  34. make.height.mas_equalTo(buttonSize.height);
  35. }];
  36. [self mas_makeConstraints:^(MASConstraintMaker *make) {
  37. make.bottom.equalTo(self.applyButton.mas_bottom).with.offset(20);
  38. }];
  39. }
  40. #pragma mark - private methods
  41. - (UIButton *)createButtonWithTitle:(NSString *)title imageNamed:(NSString *)imageNamed {
  42. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  43. button.titleLabel.font = [UIFont systemFontOfSize:12];
  44. button.titleLabel.textAlignment = NSTextAlignmentCenter;
  45. [button setImage:[UIImage imageNamed:imageNamed] forState:UIControlStateNormal];
  46. [button setTitle:title forState:UIControlStateNormal];
  47. [button setTitleColor:COLOR_BACK6 forState:UIControlStateNormal];
  48. //文字图片上下显示
  49. CGSize imageSize = CGSizeMake(HJHorizontalScale(34), HJHorizontalScale(34));
  50. CGSize titleSize = CGSizeMake(HJHorizontalScale(60), HJHorizontalScale(13));
  51. CGFloat maxWidth = MAX(imageSize.width, titleSize.width);
  52. CGFloat padding = 10;
  53. button.imageRect = CGRectMake((maxWidth - imageSize.width) / 2, 0, imageSize.width, imageSize.height);
  54. button.titleRect = CGRectMake((maxWidth - titleSize.width) / 2, imageSize.height + padding, titleSize.width, titleSize.height);
  55. button.frame = CGRectMake(0, 0, maxWidth, imageSize.height + titleSize.height + padding);
  56. return button;
  57. }
  58. #pragma mark - getters and setters
  59. - (UIButton *)favoriteButton {
  60. if (_favoriteButton == nil)
  61. {
  62. _favoriteButton = [self createButtonWithTitle:@"我的收藏" imageNamed:@"me_favorite"];
  63. }
  64. return _favoriteButton;
  65. }
  66. - (UIButton *)applyButton {
  67. if (_applyButton == nil)
  68. {
  69. _applyButton = [self createButtonWithTitle:@"我的投递" imageNamed:@"me_apply"];
  70. }
  71. return _applyButton;
  72. }
  73. - (UIButton *)invitationButton {
  74. if (_invitationButton == nil)
  75. {
  76. _invitationButton = [self createButtonWithTitle:@"面试邀请" imageNamed:@"me_invitation"];
  77. }
  78. return _invitationButton;
  79. }
  80. @end