IQToolbar.m 8.9 KB

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