IQBarButtonItem.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // IQBarButtonItem.m
  3. // https://github.com/hackiftekhar/IQKeyboardManager
  4. // Copyright (c) 2013-16 Iftekhar Qurashi.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #import <UIKit/UIKit.h>
  24. #import "IQBarButtonItem.h"
  25. #import "IQKeyboardManagerConstantsInternal.h"
  26. @implementation IQBarButtonItem
  27. -(void)initialize
  28. {
  29. NSArray <NSNumber*> *states = @[@(UIControlStateNormal),@(UIControlStateHighlighted),@(UIControlStateDisabled),@(UIControlStateFocused)];
  30. for (NSNumber *state in states)
  31. {
  32. UIControlState controlState = [state unsignedIntegerValue];
  33. [self setBackgroundImage:nil forState:controlState barMetrics:UIBarMetricsDefault];
  34. [self setBackgroundImage:nil forState:controlState style:UIBarButtonItemStylePlain barMetrics:UIBarMetricsDefault];
  35. [self setBackButtonBackgroundImage:nil forState:controlState barMetrics:UIBarMetricsDefault];
  36. }
  37. [self setTitlePositionAdjustment:UIOffsetZero forBarMetrics:UIBarMetricsDefault];
  38. [self setBackgroundVerticalPositionAdjustment:0 forBarMetrics:UIBarMetricsDefault];
  39. [self setBackButtonBackgroundVerticalPositionAdjustment:0 forBarMetrics:UIBarMetricsDefault];
  40. }
  41. - (instancetype)init {
  42. self = [super init];
  43. if (self)
  44. {
  45. [self initialize];
  46. }
  47. return self;
  48. }
  49. - (instancetype)initWithCoder:(NSCoder *)coder {
  50. self = [super initWithCoder: coder];
  51. if (self)
  52. {
  53. [self initialize];
  54. }
  55. return self;
  56. }
  57. -(void)setTintColor:(UIColor *)tintColor
  58. {
  59. [super setTintColor:tintColor];
  60. //titleTextAttributes tweak is to overcome an issue comes with iOS11 where appearanceProxy set for NSForegroundColorAttributeName and bar button texts start appearing in appearance proxy color
  61. NSMutableDictionary *textAttributes = [[self titleTextAttributesForState:UIControlStateNormal] mutableCopy]?:[NSMutableDictionary new];
  62. textAttributes[NSForegroundColorAttributeName] = tintColor;
  63. [self setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
  64. }
  65. - (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(nullable id)target action:(nullable SEL)action
  66. {
  67. self = [super initWithBarButtonSystemItem:systemItem target:target action:action];
  68. if (self)
  69. {
  70. _isSystemItem = YES;
  71. }
  72. return self;
  73. }
  74. -(void)setTarget:(nullable id)target action:(nullable SEL)action
  75. {
  76. NSInvocation *invocation = nil;
  77. if (target && action)
  78. {
  79. invocation = [NSInvocation invocationWithMethodSignature:[target methodSignatureForSelector:action]];
  80. invocation.target = target;
  81. invocation.selector = action;
  82. }
  83. self.invocation = invocation;
  84. }
  85. -(void)dealloc
  86. {
  87. self.target = nil;
  88. self.invocation = nil;
  89. }
  90. @end