From 805871a156f7d0822c7bded9202513015cb55a5a Mon Sep 17 00:00:00 2001 From: monzy613 Date: Sun, 15 May 2016 14:49:09 +0800 Subject: [PATCH] add support to addButtonWithTitle:backgroundColor:cornerRadius --- SIAlertView/SIAlertView.h | 1 + SIAlertView/SIAlertView.m | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/SIAlertView/SIAlertView.h b/SIAlertView/SIAlertView.h index e4d620a..0e65f1f 100644 --- a/SIAlertView/SIAlertView.h +++ b/SIAlertView/SIAlertView.h @@ -75,6 +75,7 @@ typedef void(^SIAlertViewHandler)(SIAlertView *alertView); - (void)setDestructiveButtonImage:(UIImage *)destructiveButtonImage forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; - (id)initWithTitle:(NSString *)title andMessage:(NSString *)message; +- (void)addButtonWithTitle:(NSString *)title type:(SIAlertViewButtonType)type backgroundColor:(UIColor *)backgroundColor cornerRadius:(CGFloat)cornerRadius handler:(SIAlertViewHandler)handler; - (void)addButtonWithTitle:(NSString *)title type:(SIAlertViewButtonType)type handler:(SIAlertViewHandler)handler; - (void)show; diff --git a/SIAlertView/SIAlertView.m b/SIAlertView/SIAlertView.m index 7a67df3..bd46e42 100644 --- a/SIAlertView/SIAlertView.m +++ b/SIAlertView/SIAlertView.m @@ -130,6 +130,8 @@ - (void)drawRect:(CGRect)rect @interface SIAlertItem : NSObject @property (nonatomic, copy) NSString *title; +@property (nonatomic, copy) UIColor *backgroundColor; +@property (nonatomic, assign) CGFloat cornerRadius; @property (nonatomic, assign) SIAlertViewButtonType type; @property (nonatomic, copy) SIAlertViewHandler action; @@ -350,12 +352,25 @@ - (void)setMessage:(NSString *)message #pragma mark - Public +- (void)addButtonWithTitle:(NSString *)title type:(SIAlertViewButtonType)type backgroundColor:(UIColor *)backgroundColor cornerRadius:(CGFloat)cornerRadius handler:(SIAlertViewHandler)handler +{ + SIAlertItem *item = [[SIAlertItem alloc] init]; + item.title = title; + item.type = type; + item.action = handler; + item.backgroundColor = backgroundColor; + item.cornerRadius = cornerRadius; + [self.items addObject:item]; +} + - (void)addButtonWithTitle:(NSString *)title type:(SIAlertViewButtonType)type handler:(SIAlertViewHandler)handler { SIAlertItem *item = [[SIAlertItem alloc] init]; item.title = title; item.type = type; item.action = handler; + item.backgroundColor = [UIColor clearColor]; + item.cornerRadius = 0.0; [self.items addObject:item]; } @@ -754,12 +769,18 @@ - (void)validateLayout CGFloat width = (self.containerView.bounds.size.width - CONTENT_PADDING_LEFT * 2 - GAP) * 0.5; UIButton *button = self.buttons[0]; button.frame = CGRectMake(CONTENT_PADDING_LEFT, y, width, BUTTON_HEIGHT); + button.backgroundColor = ((SIAlertItem *)self.items[0]).backgroundColor; + button.layer.cornerRadius = ((SIAlertItem *)self.items[0]).cornerRadius; button = self.buttons[1]; button.frame = CGRectMake(CONTENT_PADDING_LEFT + width + GAP, y, width, BUTTON_HEIGHT); + button.backgroundColor = ((SIAlertItem *)self.items[1]).backgroundColor; + button.layer.cornerRadius = ((SIAlertItem *)self.items[1]).cornerRadius; } else { for (NSUInteger i = 0; i < self.buttons.count; i++) { UIButton *button = self.buttons[i]; button.frame = CGRectMake(CONTENT_PADDING_LEFT, y, self.containerView.bounds.size.width - CONTENT_PADDING_LEFT * 2, BUTTON_HEIGHT); + button.backgroundColor = ((SIAlertItem *)self.items[i]).backgroundColor; + button.layer.cornerRadius = ((SIAlertItem *)self.items[i]).cornerRadius; if (self.buttons.count > 1) { if (i == self.buttons.count - 1 && ((SIAlertItem *)self.items[i]).type == SIAlertViewButtonTypeCancel) { CGRect rect = button.frame;