diff --git a/CXAlertView/CXAlertButtonItem.h b/CXAlertView/CXAlertButtonItem.h index 0372d07..b821e57 100644 --- a/CXAlertView/CXAlertButtonItem.h +++ b/CXAlertView/CXAlertButtonItem.h @@ -25,5 +25,6 @@ typedef void(^CXAlertButtonHandler)(CXAlertView *alertView, CXAlertButtonItem *b @property (nonatomic, assign) CXAlertViewButtonType type; @property (nonatomic, copy) CXAlertButtonHandler action; @property (nonatomic) BOOL defaultRightLineVisible; +@property (nonatomic) BOOL defaultTopLineVisible; @end diff --git a/CXAlertView/CXAlertButtonItem.m b/CXAlertView/CXAlertButtonItem.m index 16329ad..390ec44 100644 --- a/CXAlertView/CXAlertButtonItem.m +++ b/CXAlertView/CXAlertButtonItem.m @@ -32,6 +32,16 @@ - (void)drawRect:(CGRect)rect CGContextAddLineToPoint(context, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame)); CGContextStrokePath(context); } + if (_defaultTopLineVisible) { + CGContextRef context = UIGraphicsGetCurrentContext(); + CGContextClearRect(context, self.bounds); + + CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.671 green:0.675 blue:0.694 alpha:1.000].CGColor); + CGContextSetLineWidth(context, 2.0); + CGContextMoveToPoint(context, 0,0); + CGContextAddLineToPoint(context, CGRectGetWidth(self.frame), 0); + CGContextStrokePath(context); + } } @end diff --git a/CXAlertView/CXAlertView.h b/CXAlertView/CXAlertView.h index 14347ff..0f496ca 100644 --- a/CXAlertView/CXAlertView.h +++ b/CXAlertView/CXAlertView.h @@ -47,6 +47,7 @@ typedef void(^CXAlertViewHandler)(CXAlertView *alertView); @property (nonatomic, assign) CGFloat bottomScrollViewHeight; @property (nonatomic, assign) BOOL showButtonLine; @property (nonatomic, assign) BOOL showBlurBackground; +@property (nonatomic, assign) BOOL stackButtons; // Create - (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle; - (id)initWithTitle:(NSString *)title contentView:(UIView *)contentView cancelButtonTitle:(NSString *)cancelButtonTitle; diff --git a/CXAlertView/CXAlertView.m b/CXAlertView/CXAlertView.m index 33a25e8..dc770f7 100644 --- a/CXAlertView/CXAlertView.m +++ b/CXAlertView/CXAlertView.m @@ -197,6 +197,7 @@ - (id)initWithTitle:(NSString *)title contentView:(UIView *)contentView cancelBu _showButtonLine = YES; _showBlurBackground = YES; + _stackButtons = YES; [self setupScrollViews]; if (cancelButtonTitle) { [self addButtonWithTitle:cancelButtonTitle type:CXAlertViewButtonTypeCancel handler:^(CXAlertView *alertView, CXAlertButtonItem *button) { @@ -789,7 +790,8 @@ - (void)addButtonWithTitle:(NSString *)title type:(CXAlertViewButtonType)type ha [button.titleLabel setNumberOfLines:0]; button.titleLabel.lineBreakMode=BT_LBM; [button setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)]; - + + self.buttonHeight = kDefaultButtonHeight; if ([_buttons count] == 0) { @@ -798,8 +800,11 @@ - (void)addButtonWithTitle:(NSString *)title type:(CXAlertViewButtonType)type ha [_buttons addObject:button]; [self setMaxSizeForAllButtons]; + + CGFloat newContentWidth = self.bottomScrollView.contentSize.width + CGRectGetWidth(button.frame); + _bottomScrollView.contentSize = CGSizeMake( newContentWidth, _bottomScrollView.contentSize.height); } - else + else if ([_buttons count] == 1 || !_stackButtons) { // correct first button CXAlertButtonItem *firstButton = [_buttons objectAtIndex:0]; @@ -813,27 +818,62 @@ - (void)addButtonWithTitle:(NSString *)title type:(CXAlertViewButtonType)type ha button.alpha = 0.; [_buttons addObject:button]; + + void (^buttomFrameBlock)() = ^{ + firstButton.frame = newFrame; + button.alpha = 1.; + button.frame = CGRectMake( last_x, 0, self.containerWidth/2, self.buttonHeight); + [self setMaxSizeForAllButtons]; + }; if (self.isVisible) { [UIView animateWithDuration:0.3 animations:^{ - firstButton.frame = newFrame; - button.alpha = 1.; - button.frame = CGRectMake( last_x, 0, self.containerWidth/2, self.buttonHeight); - [self setMaxSizeForAllButtons]; + buttomFrameBlock(); }]; } else { - firstButton.frame = newFrame; - button.alpha = 1.; - button.frame = CGRectMake( last_x, 0, self.containerWidth/2, self.buttonHeight); + buttomFrameBlock(); + } + + CGFloat newContentWidth = self.bottomScrollView.contentSize.width + CGRectGetWidth(button.frame); + _bottomScrollView.contentSize = CGSizeMake( newContentWidth, _bottomScrollView.contentSize.height); + } + else // stack buttons if more than 2 + { + button.frame = CGRectMake(0, 0, self.containerWidth, self.buttonHeight); + button.alpha = 0.; + + [_buttons addObject:button]; + + void (^buttomFrameBlock)() = ^{ + CGFloat currentY = 0; + // correct existing buttons, set up new one as well + for (CXAlertButtonItem *buttonItem in _buttons) { + buttonItem.defaultRightLineVisible = NO; + buttonItem.defaultTopLineVisible = YES; + buttonItem.frame = CGRectMake(0, currentY, self.containerWidth, self.buttonHeight); + buttonItem.alpha = 1.; + [buttonItem setNeedsDisplay]; + currentY += self.buttonHeight; + } [self setMaxSizeForAllButtons]; + }; + + if (self.isVisible) { + [UIView animateWithDuration:0.3 animations:^{ + buttomFrameBlock(); + }]; } + else { + buttomFrameBlock(); + } + + _bottomScrollViewHeight = self.buttonHeight * _buttons.count; + _bottomScrollView.contentSize = CGSizeMake(self.bottomScrollView.contentSize.width, _bottomScrollViewHeight); + [self updateBottomScrollView]; } [_bottomScrollView addSubview:button]; - - CGFloat newContentWidth = self.bottomScrollView.contentSize.width + CGRectGetWidth(button.frame); - _bottomScrollView.contentSize = CGSizeMake( newContentWidth, _bottomScrollView.contentSize.height); } - (CXAlertButtonItem *)buttonItemWithType:(CXAlertViewButtonType)type font:(UIFont *)font