IQBarButtonItem.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 "IQBarButtonItem.h"
  24. #import "IQKeyboardManagerConstantsInternal.h"
  25. #import <UIKit/NSAttributedString.h>
  26. @implementation IQBarButtonItem
  27. +(void)initialize
  28. {
  29. [super initialize];
  30. IQBarButtonItem *appearanceProxy = [self appearance];
  31. NSArray <NSNumber*> *states = @[@(UIControlStateNormal),@(UIControlStateHighlighted),@(UIControlStateDisabled),@(UIControlStateSelected),@(UIControlStateApplication),@(UIControlStateReserved)];
  32. for (NSNumber *state in states)
  33. {
  34. UIControlState controlState = [state unsignedIntegerValue];
  35. [appearanceProxy setBackgroundImage:nil forState:controlState barMetrics:UIBarMetricsDefault];
  36. [appearanceProxy setBackgroundImage:nil forState:controlState style:UIBarButtonItemStyleDone barMetrics:UIBarMetricsDefault];
  37. [appearanceProxy setBackgroundImage:nil forState:controlState style:UIBarButtonItemStylePlain barMetrics:UIBarMetricsDefault];
  38. [appearanceProxy setBackButtonBackgroundImage:nil forState:controlState barMetrics:UIBarMetricsDefault];
  39. }
  40. [appearanceProxy setTitlePositionAdjustment:UIOffsetZero forBarMetrics:UIBarMetricsDefault];
  41. [appearanceProxy setBackgroundVerticalPositionAdjustment:0 forBarMetrics:UIBarMetricsDefault];
  42. [appearanceProxy setBackButtonBackgroundVerticalPositionAdjustment:0 forBarMetrics:UIBarMetricsDefault];
  43. }
  44. -(void)setTintColor:(UIColor *)tintColor
  45. {
  46. [super setTintColor:tintColor];
  47. //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
  48. NSMutableDictionary *textAttributes = [[self titleTextAttributesForState:UIControlStateNormal] mutableCopy]?:[NSMutableDictionary new];
  49. textAttributes[NSForegroundColorAttributeName] = tintColor;
  50. [self setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
  51. }
  52. - (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(nullable id)target action:(nullable SEL)action
  53. {
  54. self = [super initWithBarButtonSystemItem:systemItem target:target action:action];
  55. if (self)
  56. {
  57. _isSystemItem = YES;
  58. }
  59. return self;
  60. }
  61. -(void)setTarget:(nullable id)target action:(nullable SEL)action
  62. {
  63. NSInvocation *invocation = nil;
  64. if (target && action)
  65. {
  66. invocation = [NSInvocation invocationWithMethodSignature:[target methodSignatureForSelector:action]];
  67. invocation.target = target;
  68. invocation.selector = action;
  69. }
  70. self.invocation = invocation;
  71. }
  72. -(void)dealloc
  73. {
  74. self.target = nil;
  75. self.invocation.target = nil;
  76. self.invocation = nil;
  77. }
  78. @end