UIBarButtonItem+SXCreate.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // UIBarButtonItem+SXCreate.m
  3. // UINavigation-SXFixSpace
  4. //
  5. // Created by charles on 2017/9/8.
  6. // Copyright © 2017年 None. All rights reserved.
  7. //
  8. #import "UIBarButtonItem+SXCreate.h"
  9. @implementation UIBarButtonItem (SXCreate)
  10. +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(UIImage *)image {
  11. return [self itemWithTarget:target action:action nomalImage:image higeLightedImage:nil imageEdgeInsets:UIEdgeInsetsZero alignment:NSTextAlignmentLeft];
  12. }
  13. +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(UIImage *)image imageEdgeInsets:(UIEdgeInsets)imageEdgeInsets {
  14. return [self itemWithTarget:target action:action nomalImage:image higeLightedImage:nil imageEdgeInsets:imageEdgeInsets alignment:NSTextAlignmentLeft];
  15. }
  16. +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(UIImage *)image alignment:(NSTextAlignment)alignment{
  17. return [self itemWithTarget:target action:action nomalImage:image higeLightedImage:nil imageEdgeInsets:UIEdgeInsetsZero alignment:alignment];
  18. }
  19. +(UIBarButtonItem *)itemWithTarget:(id)target
  20. action:(SEL)action
  21. nomalImage:(UIImage *)nomalImage
  22. higeLightedImage:(UIImage *)higeLightedImage
  23. imageEdgeInsets:(UIEdgeInsets)imageEdgeInsets
  24. alignment:(NSTextAlignment)alignment{
  25. UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
  26. [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  27. // [button setImage:[nomalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
  28. // if (higeLightedImage) {
  29. // [button setImage:higeLightedImage forState:UIControlStateHighlighted];
  30. // }
  31. UIImageView *imgV = [[UIImageView alloc] initWithImage:[nomalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  32. [button addSubview:imgV];
  33. [button sizeToFit];
  34. if (button.bounds.size.width < 44) {
  35. CGFloat width = 44 / button.bounds.size.height * button.bounds.size.width;
  36. button.bounds = CGRectMake(0, 0, width, 44);
  37. }
  38. if (button.bounds.size.height > 44) {
  39. CGFloat height = 44 / button.bounds.size.width * button.bounds.size.height;
  40. button.bounds = CGRectMake(0, 0, 44, height);
  41. }
  42. CGFloat x = 0.0f;
  43. switch (alignment) {
  44. case NSTextAlignmentLeft:
  45. x = 0.0f;
  46. break;
  47. case NSTextAlignmentCenter:
  48. x = (button.mj_w - nomalImage.size.width)/2.0f;
  49. break;
  50. case NSTextAlignmentRight:
  51. x = button.mj_w - nomalImage.size.width;
  52. break;
  53. default:
  54. break;
  55. }
  56. imgV.frame = CGRectMake(0, (button.mj_h - nomalImage.size.height) / 2.0f, nomalImage.size.width, nomalImage.size.height);
  57. button.imageEdgeInsets = imageEdgeInsets;
  58. [button setEnlargeEdge:20];
  59. return [[UIBarButtonItem alloc] initWithCustomView:button];
  60. }
  61. +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action title:(NSString *)title {
  62. return [self itemWithTarget:target action:action title:title font:nil titleColor:nil highlightedColor:nil titleEdgeInsets:UIEdgeInsetsZero];
  63. }
  64. +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action title:(NSString *)title titleEdgeInsets:(UIEdgeInsets)titleEdgeInsets {
  65. return [self itemWithTarget:target action:action title:title font:nil titleColor:nil highlightedColor:nil titleEdgeInsets:titleEdgeInsets];
  66. }
  67. +(UIBarButtonItem *)itemWithTarget:(id)target
  68. action:(SEL)action
  69. title:(NSString *)title
  70. font:(UIFont *)font
  71. titleColor:(UIColor *)titleColor
  72. highlightedColor:(UIColor *)highlightedColor
  73. titleEdgeInsets:(UIEdgeInsets)titleEdgeInsets {
  74. UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
  75. [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  76. [button setTitle:title forState:UIControlStateNormal];
  77. button.titleLabel.font = font?font:[UIFont systemFontOfSize:15];
  78. [button setTitleColor:titleColor?titleColor:[UIColor blackColor] forState:UIControlStateNormal];
  79. [button setTitleColor:highlightedColor?highlightedColor:[UIColor blackColor] forState:UIControlStateHighlighted];
  80. [button sizeToFit];
  81. if (button.bounds.size.width < 44) {
  82. CGFloat width = 44 / button.bounds.size.height * button.bounds.size.width;
  83. button.bounds = CGRectMake(0, 0, width, 44);
  84. }
  85. if (button.bounds.size.height > 44) {
  86. CGFloat height = 44 / button.bounds.size.width * button.bounds.size.height;
  87. button.bounds = CGRectMake(0, 0, 44, height);
  88. }
  89. button.titleEdgeInsets = titleEdgeInsets;
  90. return [[UIBarButtonItem alloc] initWithCustomView:button];
  91. }
  92. +(UIBarButtonItem *)fixedSpaceWithWidth:(CGFloat)width {
  93. UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  94. fixedSpace.width = width;
  95. return fixedSpace;
  96. }
  97. @end