IQActionSheetToolbar.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // IQActionSheetToolbar.m
  3. // https://github.com/hackiftekhar/IQActionSheetPickerView
  4. // Copyright (c) 2013-14 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 "IQActionSheetToolbar.h"
  24. #import "IQActionSheetTitleBarButtonItem.h"
  25. @implementation IQActionSheetToolbar
  26. -(void)initialize
  27. {
  28. [self sizeToFit];
  29. self.autoresizingMask = UIViewAutoresizingFlexibleWidth;// | UIViewAutoresizingFlexibleHeight;
  30. self.translucent = NO;
  31. // [self setTintColor:[UIColor blackColor]];
  32. // Create a fake button to maintain flexibleSpace between cancelButton and titleLabel.(Otherwise the titleLabel will lean to the left)
  33. UIBarButtonItem *nilButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
  34. // Create a cancel button to show on keyboard to resign it. Adding a selector to resign it.
  35. // self.cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nil action:nil];
  36. if (@available(iOS 11.0, *)) {
  37. self.cancelButton = [[UIBarButtonItem alloc]initWithTitle:@" 取消" style:(UIBarButtonItemStylePlain) target:nil action:nil];
  38. self.doneButton = [[UIBarButtonItem alloc]initWithTitle:@"确定 " style:(UIBarButtonItemStylePlain) target:nil action:nil];
  39. }else{
  40. self.cancelButton = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:(UIBarButtonItemStylePlain) target:nil action:nil];
  41. self.doneButton = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:(UIBarButtonItemStylePlain) target:nil action:nil];
  42. }
  43. // self.cancelButton = [UIBarButtonItem itemWithTarget:nil action:nil title:@"取消"];
  44. // Create a title button to show on toolBar for the title you need.
  45. self.titleButton =[[IQActionSheetTitleBarButtonItem alloc] initWithTitle:nil];
  46. // Create a done button to show on keyboard to resign it. Adding a selector to resign it.
  47. // self.doneButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];
  48. // self.doneButton = [UIBarButtonItem itemWithTarget:nil action:nil title:@"确定"];
  49. // Adding button to toolBar.
  50. [self setItems:@[self.cancelButton,nilButton,self.titleButton,nilButton,self.doneButton]];
  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. -(CGSize)sizeThatFits:(CGSize)size
  71. {
  72. CGSize sizeThatFit = [super sizeThatFits:size];
  73. sizeThatFit.height = 44;
  74. return sizeThatFit;
  75. }
  76. -(void)setTintColor:(UIColor *)tintColor
  77. {
  78. [super setTintColor:tintColor];
  79. for (UIBarButtonItem *item in self.items)
  80. {
  81. [item setTintColor:tintColor];
  82. }
  83. }
  84. -(void)layoutSubviews
  85. {
  86. [super layoutSubviews];
  87. static Class IQUIToolbarTextButtonClass;
  88. static dispatch_once_t onceToken;
  89. dispatch_once(&onceToken, ^{
  90. IQUIToolbarTextButtonClass = NSClassFromString(@"UIToolbarTextButton");
  91. });
  92. NSArray *subviews = [self.subviews sortedArrayUsingComparator:^NSComparisonResult(UIView *view1, UIView *view2) {
  93. CGFloat x1 = CGRectGetMinX(view1.frame);
  94. CGFloat y1 = CGRectGetMinY(view1.frame);
  95. CGFloat x2 = CGRectGetMinX(view2.frame);
  96. CGFloat y2 = CGRectGetMinY(view2.frame);
  97. if (x1 < x2) return NSOrderedAscending;
  98. else if (x1 > x2) return NSOrderedDescending;
  99. //Else both y are same so checking for x positions
  100. else if (y1 < y2) return NSOrderedAscending;
  101. else if (y1 > y2) return NSOrderedDescending;
  102. else return NSOrderedSame;
  103. }];
  104. UIView *leftBarButtonView = nil;
  105. UIView *titleBarButtonView = nil;
  106. UIView *rightBarButtonView = nil;
  107. for (UIView *barButtonItemView in subviews)
  108. {
  109. if (titleBarButtonView != nil && [barButtonItemView isKindOfClass:IQUIToolbarTextButtonClass])
  110. {
  111. rightBarButtonView = barButtonItemView;
  112. }
  113. else if (titleBarButtonView == nil && [barButtonItemView isMemberOfClass:[UIView class]])
  114. {
  115. titleBarButtonView = barButtonItemView;
  116. }
  117. else if (leftBarButtonView == nil && [barButtonItemView isKindOfClass:IQUIToolbarTextButtonClass])
  118. {
  119. leftBarButtonView = barButtonItemView;
  120. }
  121. }
  122. /*Left Bar Button Item*/
  123. CGRect rect = leftBarButtonView.frame;
  124. rect.origin.x = 8;
  125. leftBarButtonView.frame = rect;
  126. /*Right Bar Button Item*/
  127. rect = rightBarButtonView.frame;
  128. rect.origin.x = self.frame.size.width-rect.size.width-8;
  129. rightBarButtonView.frame = rect;
  130. /*Title Bar Button Item*/
  131. CGFloat x = CGRectGetMaxX(leftBarButtonView.frame) + 24;
  132. CGFloat width = (CGRectGetMinX(rightBarButtonView.frame)-24)-x;
  133. rect = titleBarButtonView.frame;
  134. rect.origin.x = x;
  135. rect.origin.y = 0;
  136. rect.size.width = width;
  137. rect.size.height = self.frame.size.height;
  138. titleBarButtonView.frame = rect;
  139. }
  140. @end