悟空记账

IQToolbar.m 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //
  2. // IQToolbar.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 "IQToolbar.h"
  24. #import "IQKeyboardManagerConstantsInternal.h"
  25. #import "IQUIView+Hierarchy.h"
  26. #import <UIKit/UIAccessibility.h>
  27. #import <UIKit/UIViewController.h>
  28. @implementation IQToolbar
  29. @synthesize previousBarButton = _previousBarButton;
  30. @synthesize nextBarButton = _nextBarButton;
  31. @synthesize titleBarButton = _titleBarButton;
  32. @synthesize doneBarButton = _doneBarButton;
  33. @synthesize fixedSpaceBarButton = _fixedSpaceBarButton;
  34. +(void)initialize
  35. {
  36. [super initialize];
  37. IQToolbar *appearanceProxy = [self appearance];
  38. NSArray <NSNumber*> *positions = @[@(UIBarPositionAny),@(UIBarPositionBottom),@(UIBarPositionTop),@(UIBarPositionTopAttached)];
  39. for (NSNumber *position in positions)
  40. {
  41. UIToolbarPosition toolbarPosition = [position unsignedIntegerValue];
  42. [appearanceProxy setBackgroundImage:nil forToolbarPosition:toolbarPosition barMetrics:UIBarMetricsDefault];
  43. [appearanceProxy setShadowImage:nil forToolbarPosition:toolbarPosition];
  44. }
  45. }
  46. -(void)initialize
  47. {
  48. [self sizeToFit];
  49. self.autoresizingMask = UIViewAutoresizingFlexibleWidth;// | UIViewAutoresizingFlexibleHeight;
  50. self.translucent = YES;
  51. }
  52. - (instancetype)initWithFrame:(CGRect)frame
  53. {
  54. self = [super initWithFrame:frame];
  55. if (self)
  56. {
  57. [self initialize];
  58. }
  59. return self;
  60. }
  61. - (instancetype)initWithCoder:(NSCoder *)coder
  62. {
  63. self = [super initWithCoder:coder];
  64. if (self)
  65. {
  66. [self initialize];
  67. }
  68. return self;
  69. }
  70. -(void)dealloc
  71. {
  72. self.items = nil;
  73. _previousBarButton = nil;
  74. _nextBarButton = nil;
  75. _titleBarButton = nil;
  76. _doneBarButton = nil;
  77. _fixedSpaceBarButton = nil;
  78. }
  79. -(IQBarButtonItem *)previousBarButton
  80. {
  81. if (_previousBarButton == nil)
  82. {
  83. _previousBarButton = [[IQBarButtonItem alloc] initWithImage:nil style:UIBarButtonItemStylePlain target:nil action:nil];
  84. _previousBarButton.accessibilityLabel = @"Toolbar Previous Button";
  85. }
  86. return _previousBarButton;
  87. }
  88. -(IQBarButtonItem *)nextBarButton
  89. {
  90. if (_nextBarButton == nil)
  91. {
  92. _nextBarButton = [[IQBarButtonItem alloc] initWithImage:nil style:UIBarButtonItemStylePlain target:nil action:nil];
  93. _nextBarButton.accessibilityLabel = @"Toolbar Next Button";
  94. }
  95. return _nextBarButton;
  96. }
  97. -(IQTitleBarButtonItem *)titleBarButton
  98. {
  99. if (_titleBarButton == nil)
  100. {
  101. _titleBarButton = [[IQTitleBarButtonItem alloc] initWithTitle:nil];
  102. _titleBarButton.accessibilityLabel = @"Toolbar Title Button";
  103. }
  104. return _titleBarButton;
  105. }
  106. -(IQBarButtonItem *)doneBarButton
  107. {
  108. if (_doneBarButton == nil)
  109. {
  110. _doneBarButton = [[IQBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];
  111. _doneBarButton.accessibilityLabel = @"Toolbar Done Button";
  112. }
  113. return _doneBarButton;
  114. }
  115. -(IQBarButtonItem *)fixedSpaceBarButton
  116. {
  117. if (_fixedSpaceBarButton == nil)
  118. {
  119. _fixedSpaceBarButton = [[IQBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  120. }
  121. return _fixedSpaceBarButton;
  122. }
  123. -(CGSize)sizeThatFits:(CGSize)size
  124. {
  125. CGSize sizeThatFit = [super sizeThatFits:size];
  126. sizeThatFit.height = 44;
  127. return sizeThatFit;
  128. }
  129. -(void)setBarStyle:(UIBarStyle)barStyle
  130. {
  131. [super setBarStyle:barStyle];
  132. if (barStyle == UIBarStyleDefault)
  133. {
  134. [self.titleBarButton setSelectableTextColor:[UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:1.0]];
  135. }
  136. else
  137. {
  138. [self.titleBarButton setSelectableTextColor:[UIColor yellowColor]];
  139. }
  140. }
  141. -(void)setTintColor:(UIColor *)tintColor
  142. {
  143. [super setTintColor:tintColor];
  144. for (UIBarButtonItem *item in self.items)
  145. {
  146. [item setTintColor:tintColor];
  147. }
  148. }
  149. -(void)layoutSubviews
  150. {
  151. [super layoutSubviews];
  152. //If running on Xcode9 (iOS11) only then we'll validate for iOS version, otherwise for older versions of Xcode (iOS10 and below) we'll just execute the tweak
  153. #ifdef __IPHONE_11_0
  154. if (@available(iOS 11.0, *)) {}
  155. else
  156. #endif
  157. {
  158. CGRect leftRect = CGRectNull;
  159. CGRect rightRect = CGRectNull;
  160. BOOL isTitleBarButtonFound = NO;
  161. NSArray *subviews = [self.subviews sortedArrayUsingComparator:^NSComparisonResult(UIView *view1, UIView *view2) {
  162. CGFloat x1 = CGRectGetMinX(view1.frame);
  163. CGFloat y1 = CGRectGetMinY(view1.frame);
  164. CGFloat x2 = CGRectGetMinX(view2.frame);
  165. CGFloat y2 = CGRectGetMinY(view2.frame);
  166. if (x1 < x2) return NSOrderedAscending;
  167. else if (x1 > x2) return NSOrderedDescending;
  168. //Else both y are same so checking for x positions
  169. else if (y1 < y2) return NSOrderedAscending;
  170. else if (y1 > y2) return NSOrderedDescending;
  171. else return NSOrderedSame;
  172. }];
  173. for (UIView *barButtonItemView in subviews)
  174. {
  175. if (isTitleBarButtonFound == YES)
  176. {
  177. rightRect = barButtonItemView.frame;
  178. break;
  179. }
  180. else if (barButtonItemView == self.titleBarButton.customView)
  181. {
  182. isTitleBarButtonFound = YES;
  183. }
  184. //If it's UIToolbarButton or UIToolbarTextButton (which actually UIBarButtonItem)
  185. else if ([barButtonItemView isKindOfClass:[UIControl class]])
  186. {
  187. leftRect = barButtonItemView.frame;
  188. }
  189. }
  190. CGFloat titleMargin = 16;
  191. CGFloat maxWidth = CGRectGetWidth(self.frame) - titleMargin*2 - (CGRectIsNull(leftRect)?0:CGRectGetMaxX(leftRect)) - (CGRectIsNull(rightRect)?0:CGRectGetWidth(self.frame)-CGRectGetMinX(rightRect));
  192. CGFloat maxHeight = self.frame.size.height;
  193. CGSize sizeThatFits = [self.titleBarButton.customView sizeThatFits:CGSizeMake(maxWidth, maxHeight)];
  194. CGRect titleRect = CGRectZero;
  195. CGFloat x = titleMargin;
  196. if (sizeThatFits.width > 0 && sizeThatFits.height > 0)
  197. {
  198. CGFloat width = MIN(sizeThatFits.width, maxWidth);
  199. CGFloat height = MIN(sizeThatFits.height, maxHeight);
  200. if (CGRectIsNull(leftRect) == false)
  201. {
  202. x = titleMargin + CGRectGetMaxX(leftRect) + ((maxWidth - width)/2);
  203. }
  204. CGFloat y = (maxHeight - height)/2;
  205. titleRect = CGRectMake(x, y, width, height);
  206. }
  207. else
  208. {
  209. if (CGRectIsNull(leftRect) == false)
  210. {
  211. x = titleMargin + CGRectGetMaxX(leftRect);
  212. }
  213. CGFloat width = CGRectGetWidth(self.frame) - titleMargin*2 - (CGRectIsNull(leftRect)?0:CGRectGetMaxX(leftRect)) - (CGRectIsNull(rightRect)?0:CGRectGetWidth(self.frame)-CGRectGetMinX(rightRect));
  214. titleRect = CGRectMake(x, 0, width, maxHeight);
  215. }
  216. self.titleBarButton.customView.frame = titleRect;
  217. }
  218. }
  219. #pragma mark - UIInputViewAudioFeedback delegate
  220. - (BOOL) enableInputClicksWhenVisible
  221. {
  222. return YES;
  223. }
  224. @end