Skip to content

Commit

Permalink
added an accessoryView to show any content in SIAlertView; tweaking t…
Browse files Browse the repository at this point in the history
…he margins
  • Loading branch information
Richard H Fung committed Oct 22, 2013
1 parent 5f1dc5f commit 1b87c33
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 28 deletions.
1 change: 1 addition & 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, strong) UIView* accessoryView;

@property (nonatomic, assign) SIAlertViewTransitionStyle transitionStyle; // default is SIAlertViewTransitionStyleSlideFromBottom
@property (nonatomic, assign) SIAlertViewBackgroundStyle backgroundStyle; // default is SIAlertViewButtonTypeGradient
Expand Down
122 changes: 94 additions & 28 deletions SIAlertView/SIAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@

#define DEBUG_LAYOUT 0

#define MESSAGE_MIN_LINE_COUNT 3
#define MESSAGE_MIN_LINE_COUNT 1
#define MESSAGE_MAX_LINE_COUNT 5
#define GAP 15
#define GAP 12
#define CANCEL_BUTTON_PADDING_TOP 21
#define CONTENT_PADDING_LEFT 12
#define CONTENT_PADDING_TOP 20
#define CONTENT_PADDING_BOTTOM 12
#define BUTTON_HEIGHT 45
#define CONTAINER_WIDTH 270
#define ACCESSORY_MAX_HEIGHT 400

const UIWindowLevel UIWindowLevelSIAlert = 1999.0; // don't overlap system's alert
const UIWindowLevel UIWindowLevelSIAlertBackground = 1998.0; // below the alert window
Expand Down Expand Up @@ -103,6 +104,7 @@ @interface SIAlertView ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UIView* accessoryPlaceholderView; // contains accessoryView, if any

@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) NSMutableArray *buttons;
Expand Down Expand Up @@ -314,7 +316,7 @@ - (id)initWithTitle:(NSString *)title andMessage:(NSString *)message
self = [super init];
if (self) {
_title = title;
_message = message;
_message = message;
self.items = [[NSMutableArray alloc] init];
}
return self;
Expand Down Expand Up @@ -395,6 +397,11 @@ - (void)setMessage:(NSString *)message
[self invalidateLayout];
}

-(void)setAccessoryView:(UIView *)newAccessoryView{
_accessoryView = newAccessoryView;
[self invalidateLayout];
}

#pragma mark - Public

- (void)addButtonWithTitle:(NSString *)title type:(SIAlertViewButtonType)type handler:(SIAlertViewHandler)handler
Expand Down Expand Up @@ -467,6 +474,10 @@ - (void)show
self.didShowHandler(self);
}
[[NSNotificationCenter defaultCenter] postNotificationName:SIAlertViewDidShowNotification object:self userInfo:nil];
// start animation in accessoryView, if any, usually an UIImageView
if ([self.accessoryView respondsToSelector:@selector(startAnimating)]){
[self.accessoryView performSelector:@selector(startAnimating)];
}

[SIAlertView setAnimating:NO];

Expand Down Expand Up @@ -772,12 +783,37 @@ - (void)validateLayout
self.containerView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.containerView.bounds cornerRadius:self.containerView.layer.cornerRadius].CGPath;

CGFloat y = CONTENT_PADDING_TOP;
if (self.titleLabel) {
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);
y += height;
}

if (self.accessoryPlaceholderView){
[[self.accessoryPlaceholderView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
if (self.accessoryView){
CGSize size = self.accessoryView.frame.size;
self.accessoryView.frame = CGRectMake(0, 0, size.width, size.height);
[self.accessoryPlaceholderView addSubview:self.accessoryView];

// accessory placeholder view clips the contained view
if (size.width > self.containerView.bounds.size.width - CONTENT_PADDING_LEFT * 2)
size.width = self.containerView.bounds.size.width - CONTENT_PADDING_LEFT * 2;

if (size.height > ACCESSORY_MAX_HEIGHT) // TODO: clip the height smarter
size.height = ACCESSORY_MAX_HEIGHT;

self.accessoryPlaceholderView.frame = CGRectMake(self.containerView.bounds.size.width / 2 - size.width / 2, y, size.width, size.height);
y += size.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);
y += height;
}

if (self.messageLabel) {
if (y > CONTENT_PADDING_TOP) {
y += GAP;
Expand All @@ -787,6 +823,7 @@ - (void)validateLayout
self.messageLabel.frame = CGRectMake(CONTENT_PADDING_LEFT, y, self.containerView.bounds.size.width - CONTENT_PADDING_LEFT * 2, height);
y += height;
}

if (self.items.count > 0) {
if (y > CONTENT_PADDING_TOP) {
y += GAP;
Expand Down Expand Up @@ -820,26 +857,32 @@ - (CGFloat)preferredHeight
if (self.title) {
height += [self heightForTitleLabel];
}
if (self.message) {
if (height > CONTENT_PADDING_TOP) {
height += GAP;
}
height += [self heightForMessageLabel];
if (self.accessoryView){
if (height > CONTENT_PADDING_TOP) {
height += GAP;
}
if (self.items.count > 0) {
if (height > CONTENT_PADDING_TOP) {
height += GAP;
}
if (self.items.count <= 2 && self.buttonsListStyle == SIAlertViewButtonsListStyleNormal) {
height += BUTTON_HEIGHT;
} else {
height += (BUTTON_HEIGHT + GAP) * self.items.count - GAP;
if (self.buttons.count > 2 && ((SIAlertItem *)[self.items lastObject]).type == SIAlertViewButtonTypeCancel) {
height += CANCEL_BUTTON_PADDING_TOP;
}
}
}
height += CONTENT_PADDING_BOTTOM;
height += MIN(self.accessoryView.frame.size.height, ACCESSORY_MAX_HEIGHT);
}
if (self.message) {
if (height > CONTENT_PADDING_TOP) {
height += GAP;
}
height += [self heightForMessageLabel];
}
if (self.items.count > 0) {
if (height > CONTENT_PADDING_TOP) {
height += GAP;
}
if (self.items.count <= 2 && self.buttonsListStyle == SIAlertViewButtonsListStyleNormal) {
height += BUTTON_HEIGHT;
} else {
height += (BUTTON_HEIGHT + GAP) * self.items.count - GAP;
if (self.buttons.count > 2 && ((SIAlertItem *)[self.items lastObject]).type == SIAlertViewButtonTypeCancel) {
height += CANCEL_BUTTON_PADDING_TOP;
}
}
}
height += CONTENT_PADDING_BOTTOM;
return roundf(height);
}

Expand Down Expand Up @@ -875,6 +918,7 @@ - (void)setup
[self setupContainerView];
[self updateTitleLabel];
[self updateMessageLabel];
[self updateAccessoryView];
[self setupButtons];
[self invalidateLayout];
}
Expand Down Expand Up @@ -969,6 +1013,28 @@ - (void)updateMessageLabel
[self invalidateLayout];
}

-(void)updateAccessoryView
{
if (self.accessoryView) {
if (!self.accessoryPlaceholderView) {
CGSize size = self.accessoryView.frame.size;
self.accessoryPlaceholderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
self.accessoryPlaceholderView.backgroundColor = [UIColor clearColor];
self.accessoryPlaceholderView.clipsToBounds = YES;
[self.containerView addSubview:self.accessoryPlaceholderView];
}

// remove all old views
[[self.accessoryPlaceholderView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

// add in the new view
[self.accessoryPlaceholderView addSubview:self.accessoryView];
}else{
[self.accessoryPlaceholderView removeFromSuperview];
self.accessoryPlaceholderView = nil;
}
}

- (void)setupButtons
{
self.buttons = [[NSMutableArray alloc] initWithCapacity:self.items.count];
Expand Down

0 comments on commit 1b87c33

Please sign in to comment.