From 00e71ccf11b4309c7ff9099ccfb91f2bb70c4529 Mon Sep 17 00:00:00 2001 From: Simon Burbidge Date: Sun, 6 Apr 2014 08:29:33 +0800 Subject: [PATCH 1/2] Added "iconImageView" for an optional icon centred above the title - use "setIcon" to set the UIImage. --- SIAlertView/SIAlertView.h | 1 + SIAlertView/SIAlertView.m | 43 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/SIAlertView/SIAlertView.h b/SIAlertView/SIAlertView.h index d95c697..e523718 100644 --- a/SIAlertView/SIAlertView.h +++ b/SIAlertView/SIAlertView.h @@ -44,6 +44,7 @@ typedef void(^SIAlertViewHandler)(SIAlertView *alertView); @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *message; +@property (nonatomic, copy) UIImage *icon; @property (nonatomic, assign) SIAlertViewTransitionStyle transitionStyle; // default is SIAlertViewTransitionStyleSlideFromBottom @property (nonatomic, assign) SIAlertViewBackgroundStyle backgroundStyle; // default is SIAlertViewButtonTypeGradient diff --git a/SIAlertView/SIAlertView.m b/SIAlertView/SIAlertView.m index 511121a..ec082ec 100644 --- a/SIAlertView/SIAlertView.m +++ b/SIAlertView/SIAlertView.m @@ -49,6 +49,7 @@ @interface SIAlertView () @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UILabel *messageLabel; +@property (nonatomic, strong) UIImageView *iconImageView; @property (nonatomic, strong) UIView *containerView; @property (nonatomic, strong) NSMutableArray *buttons; @@ -341,6 +342,12 @@ - (void)setMessage:(NSString *)message [self invalidateLayout]; } +- (void)setIcon:(UIImage *)icon +{ + _icon = icon; + [self invalidateLayout]; +} + #pragma mark - Public - (void)addButtonWithTitle:(NSString *)title type:(SIAlertViewButtonType)type handler:(SIAlertViewHandler)handler @@ -724,7 +731,16 @@ - (void)validateLayout self.containerView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.containerView.bounds cornerRadius:self.containerView.layer.cornerRadius].CGPath; CGFloat y = CONTENT_PADDING_TOP; + if (self.iconImageView) { + self.iconImageView.image = self.icon; + CGFloat height = self.icon.size.height; + self.iconImageView.frame = CGRectMake(CONTENT_PADDING_LEFT, y, self.containerView.bounds.size.width - CONTENT_PADDING_LEFT * 2, height); + y += height; + } if (self.titleLabel) { + if (y > CONTENT_PADDING_TOP) { + y += GAP; + } self.titleLabel.text = self.title; CGFloat height = [self heightForTitleLabel]; self.titleLabel.frame = CGRectMake(CONTENT_PADDING_LEFT, y, self.containerView.bounds.size.width - CONTENT_PADDING_LEFT * 2, height); @@ -769,7 +785,13 @@ - (void)validateLayout - (CGFloat)preferredHeight { CGFloat height = CONTENT_PADDING_TOP; + if (self.icon) { + height += self.icon.size.height; + } if (self.title) { + if (height > CONTENT_PADDING_TOP) { + height += GAP; + } height += [self heightForTitleLabel]; } if (self.message) { @@ -833,6 +855,7 @@ - (void)setup [self setupContainerView]; [self updateTitleLabel]; [self updateMessageLabel]; + [self updateIconImageView]; [self setupButtons]; [self invalidateLayout]; } @@ -841,6 +864,7 @@ - (void)teardown { [self.containerView removeFromSuperview]; self.containerView = nil; + self.iconImageView = nil; self.titleLabel = nil; self.messageLabel = nil; [self.buttons removeAllObjects]; @@ -860,6 +884,25 @@ - (void)setupContainerView [self addSubview:self.containerView]; } +- (void)updateIconImageView +{ + if (self.icon) { + if (!self.iconImageView) { + self.iconImageView = [[UIImageView alloc] initWithFrame:self.bounds]; + [self.iconImageView setContentMode:UIViewContentModeScaleAspectFit]; + [self.containerView addSubview:self.iconImageView]; +#if DEBUG_LAYOUT + self.titleLabel.backgroundColor = [UIColor redColor]; +#endif + } + self.iconImageView.image = self.icon; + } else { + [self.iconImageView removeFromSuperview]; + self.iconImageView = nil; + } + [self invalidateLayout]; +} + - (void)updateTitleLabel { if (self.title) { From 4aa3683c10877132beb71270e5111d522f893f27 Mon Sep 17 00:00:00 2001 From: Tatsuya Tanaka Date: Wed, 12 Aug 2015 02:21:21 +0900 Subject: [PATCH 2/2] Add ability to change background color of buttons --- SIAlertView/SIAlertView.h | 3 +++ SIAlertView/SIAlertView.m | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/SIAlertView/SIAlertView.h b/SIAlertView/SIAlertView.h index e4d620a..cb900b4 100644 --- a/SIAlertView/SIAlertView.h +++ b/SIAlertView/SIAlertView.h @@ -67,6 +67,9 @@ typedef void(^SIAlertViewHandler)(SIAlertView *alertView); @property (nonatomic, strong) UIColor *buttonColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *cancelButtonColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *destructiveButtonColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; +@property (nonatomic, strong) UIColor *buttonBackgroundColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; +@property (nonatomic, strong) UIColor *cancelButtonBackgroundColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; +@property (nonatomic, strong) UIColor *destructiveButtonBackgroundColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; @property (nonatomic, assign) CGFloat cornerRadius NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; // default is 2.0 @property (nonatomic, assign) CGFloat shadowRadius NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; // default is 8.0 diff --git a/SIAlertView/SIAlertView.m b/SIAlertView/SIAlertView.m index 7a67df3..8b32f75 100644 --- a/SIAlertView/SIAlertView.m +++ b/SIAlertView/SIAlertView.m @@ -1135,6 +1135,32 @@ - (void)setDestructiveButtonColor:(UIColor *)buttonColor [self setColor:buttonColor toButtonsOfType:SIAlertViewButtonTypeDestructive]; } +- (void)setButtonBackgroundColor:(UIColor *)backgroundColor +{ + if (_buttonBackgroundColor == backgroundColor) { + return; + } + _buttonBackgroundColor = backgroundColor; + [self setBackgroundColor:backgroundColor toButtonsOfType:SIAlertViewButtonTypeDefault]; +} + +- (void)setCancelButtonBackgroundColor:(UIColor *)backgroundColor +{ + if (_cancelButtonBackgroundColor == backgroundColor) { + return; + } + _cancelButtonBackgroundColor = backgroundColor; + [self setBackgroundColor:backgroundColor toButtonsOfType:SIAlertViewButtonTypeCancel]; +} + +- (void)setDestructiveButtonBackgroundColor:(UIColor *)backgroundColor +{ + if (_destructiveButtonBackgroundColor == backgroundColor) { + return; + } + _destructiveButtonBackgroundColor = backgroundColor; + [self setBackgroundColor:backgroundColor toButtonsOfType:SIAlertViewButtonTypeDestructive]; +} - (void)setDefaultButtonImage:(UIImage *)defaultButtonImage forState:(UIControlState)state { @@ -1179,6 +1205,16 @@ -(void)setColor:(UIColor *)color toButtonsOfType:(SIAlertViewButtonType)type { } } +-(void)setBackgroundColor:(UIColor *)color toButtonsOfType:(SIAlertViewButtonType)type { + for (NSUInteger i = 0; i < self.items.count; i++) { + SIAlertItem *item = self.items[i]; + if(item.type == type) { + UIButton *button = self.buttons[i]; + button.backgroundColor = color; + } + } +} + # pragma mark - # pragma mark Enable parallax effect (iOS7 only)