@@ -13,7 +13,6 @@ @interface RFKeyboardToolbar ()
1313@property (nonatomic ,strong ) UIView *toolbarView;
1414@property (nonatomic ,strong ) UIScrollView *scrollView;
1515@property (nonatomic ,strong ) CALayer *topBorder;
16- @property (nonatomic ,strong ) NSArray *buttonsToAdd;
1716
1817@end
1918
@@ -26,7 +25,7 @@ + (instancetype)toolbarViewWithButtons:(NSArray *)buttons {
2625- (id )initWithButtons : (NSArray *)buttons {
2726 self = [super initWithFrame: CGRectMake (0 , 0 , self .window.rootViewController.view.bounds.size.width, 40 )];
2827 if (self) {
29- _buttonsToAdd = buttons;
28+ _buttons = [ buttons copy ] ;
3029 self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
3130 [self addSubview: [self inputAccessoryView ]];
3231 }
@@ -62,12 +61,19 @@ - (UIScrollView*)fakeToolbar {
6261 _scrollView.showsHorizontalScrollIndicator = NO ;
6362 _scrollView.contentInset = UIEdgeInsetsMake (6 .0f , 0 .0f , 8 .0f , 6 .0f );
6463
64+ [self addButtons ];
65+
66+ return _scrollView;
67+ }
68+
69+ - (void )addButtons
70+ {
6571 NSUInteger index = 0 ;
6672 NSUInteger originX = 8 ;
6773
6874 CGRect originFrame;
6975
70- for (RFToolbarButton *eachButton in _buttonsToAdd ) {
76+ for (RFToolbarButton *eachButton in _buttons ) {
7177 originFrame = CGRectMake (originX, 0 , eachButton.frame .size .width , eachButton.frame .size .height );
7278 eachButton.frame = originFrame;
7379
@@ -80,8 +86,66 @@ - (UIScrollView*)fakeToolbar {
8086 CGSize contentSize = _scrollView.contentSize ;
8187 contentSize.width = originX - 8 ;
8288 _scrollView.contentSize = contentSize;
89+ }
90+
91+ - (void )setButtons : (NSArray *)buttons
92+ {
93+ [_buttons makeObjectsPerformSelector: @selector (removeFromSuperview )];
94+ _buttons = [buttons copy ];
95+ [self addButtons ];
96+ }
97+
98+ - (void )setButtons : (NSArray *)buttons animated : (BOOL )animated
99+ {
100+ if (animated == NO )
101+ {
102+ self.buttons = buttons;
103+ return ;
104+ }
83105
84- return _scrollView;
106+ NSMutableSet *removeButtons = [NSMutableSet setWithArray: _buttons];
107+ [removeButtons minusSet: [NSSet setWithArray: buttons]];
108+ NSMutableSet *addButtons = [NSMutableSet setWithArray: buttons];
109+ [addButtons minusSet: [NSSet setWithArray: _buttons]];
110+ _buttons = [buttons copy ];
111+
112+ // calculate end frames
113+ NSUInteger originX = 8 ;
114+ NSUInteger index = 0 ;
115+ NSMutableArray *buttonFrames = [NSMutableArray arrayWithCapacity: _buttons.count];
116+ for (RFToolbarButton *button in _buttons) {
117+ CGRect frame = CGRectMake (originX, 0 , button.frame .size .width , button.frame .size .height );
118+ [buttonFrames addObject: [NSValue valueWithCGRect: frame]];
119+
120+ originX += button.bounds .size .width + 8 ;
121+ index++;
122+ }
123+
124+ CGSize contentSize = _scrollView.contentSize ;
125+ contentSize.width = originX - 8 ;
126+ if (contentSize.width > _scrollView.contentSize .width )
127+ _scrollView.contentSize = contentSize;
128+
129+ // make added buttons appear from the right
130+ [addButtons enumerateObjectsUsingBlock: ^(RFToolbarButton *button, BOOL *stop) {
131+ button.frame = CGRectMake (originX, 0 , button.frame .size .width , button.frame .size .height );
132+ [_scrollView addSubview: button];
133+ }];
134+
135+ // animate
136+ [UIView animateWithDuration: 0.2 animations: ^{
137+ [removeButtons enumerateObjectsUsingBlock: ^(RFToolbarButton *button, BOOL *stop) {
138+ button.alpha = 0 ;
139+ }];
140+
141+ [_buttons enumerateObjectsUsingBlock: ^(RFToolbarButton *button, NSUInteger idx, BOOL *stop) {
142+ button.frame = [buttonFrames[idx] CGRectValue ];
143+ }];
144+
145+ _scrollView.contentSize = contentSize;
146+ } completion: ^(BOOL finished) {
147+ [removeButtons makeObjectsPerformSelector: @selector (removeFromSuperview )];
148+ }];
85149}
86150
87151@end
0 commit comments