Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "iconImageView" for an optional icon centred above the title #66

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions SIAlertView/SIAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 SIAlertViewBackgroundStyleGradient
Expand All @@ -67,6 +68,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

Expand Down
79 changes: 79 additions & 0 deletions SIAlertView/SIAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -348,6 +349,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
Expand Down Expand Up @@ -731,7 +738,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);
Expand Down Expand Up @@ -776,7 +792,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) {
Expand Down Expand Up @@ -877,6 +899,7 @@ - (void)setup
[self setupContainerView];
[self updateTitleLabel];
[self updateMessageLabel];
[self updateIconImageView];
[self setupButtons];
[self invalidateLayout];
}
Expand All @@ -885,6 +908,7 @@ - (void)teardown
{
[self.containerView removeFromSuperview];
self.containerView = nil;
self.iconImageView = nil;
self.titleLabel = nil;
self.messageLabel = nil;
[self.buttons removeAllObjects];
Expand All @@ -904,6 +928,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) {
Expand Down Expand Up @@ -1135,6 +1178,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
{
Expand Down Expand Up @@ -1179,6 +1248,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)

Expand Down