Skip to content

Commit 5d8ae7b

Browse files
author
Jose Antonio Espín Mora
committed
Feature to show buttons horizontally.
1 parent 2ca8876 commit 5d8ae7b

4 files changed

Lines changed: 46 additions & 5 deletions

File tree

SCLAlertView/SCLAlertView.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ typedef NS_ENUM(NSInteger, SCLAlertViewBackground)
222222
*/
223223
@property (nonatomic) UIStatusBarStyle statusBarStyle;
224224

225+
/** Set horizontal alignment for buttons
226+
*
227+
* Horizontal aligment instead of vertically if YES
228+
*/
229+
@property (nonatomic) BOOL horizontalButtons;
230+
225231
/** Initialize SCLAlertView using a new window.
226232
*
227233
* Init with new window

SCLAlertView/SCLAlertView.m

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,17 @@ - (void)viewWillLayoutSubviews
380380
}
381381

382382
// Buttons
383+
CGFloat x = 12.0f;
383384
for (SCLButton *btn in _buttons)
384385
{
385-
btn.frame = CGRectMake(12.0f, y, btn.frame.size.width, btn.frame.size.height);
386-
y += btn.frame.size.height + 10.0f;
386+
btn.frame = CGRectMake(x, y, btn.frame.size.width, btn.frame.size.height);
387+
388+
// Add horizontal or vertical offset acording on _horizontalButtons parameter
389+
if (_horizontalButtons) {
390+
x += btn.frame.size.width + 10.0f;
391+
} else {
392+
y += btn.frame.size.height + 10.0f;
393+
}
387394
}
388395

389396
// Adapt window height according to icon size
@@ -684,12 +691,24 @@ - (SCLButton *)addButton:(NSString *)title
684691
[btn setTitle:title forState:UIControlStateNormal];
685692
btn.titleLabel.font = [UIFont fontWithName:_buttonsFontFamily size:_buttonsFontSize];
686693

687-
// Update view height
688-
self.windowHeight += (btn.frame.size.height + ADD_BUTTON_PADDING);
689-
690694
[_contentView addSubview:btn];
691695
[_buttons addObject:btn];
692696

697+
if (_horizontalButtons) {
698+
// Update buttons width according to the number of buttons
699+
for (SCLButton *bttn in _buttons) {
700+
[bttn adjustWidthWithWindowWidth:self.windowWidth numberOfButtons:[_buttons count]];
701+
}
702+
703+
// Update view height
704+
if (!([_buttons count] > 1)) {
705+
self.windowHeight += (btn.frame.size.height + ADD_BUTTON_PADDING);
706+
}
707+
} else {
708+
// Update view height
709+
self.windowHeight += (btn.frame.size.height + ADD_BUTTON_PADDING);
710+
}
711+
693712
return btn;
694713
}
695714

SCLAlertView/SCLButton.h

100644100755
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,12 @@ typedef NS_ENUM(NSInteger, SCLActionType)
9898
*/
9999
- (instancetype)initWithWindowWidth:(CGFloat)windowWidth;
100100

101+
/** Adjust width of the button according to the width of the alert and
102+
* the number of buttons. Only used when buttons are horizontally aligned.
103+
*
104+
* @param windowWith The width of the alert.
105+
* @param numberOfButtons The number of buttons in the alert.
106+
*/
107+
- (void)adjustWidthWithWindowWidth:(CGFloat)windowWidht numberOfButtons:(NSUInteger)numberOfButtons;
108+
101109
@end

SCLAlertView/SCLButton.m

100644100755
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ - (void)setupWithWindowWidth:(CGFloat)windowWidth
6363
self.layer.cornerRadius = 3.0f;
6464
}
6565

66+
- (void)adjustWidthWithWindowWidth:(CGFloat)windowWidht numberOfButtons:(NSUInteger)numberOfButtons
67+
{
68+
CGFloat allButtonsWidth = windowWidht - (MARGIN_BUTTON * 2);
69+
CGFloat buttonWidth = (allButtonsWidth - ((numberOfButtons - 1) * 10)) / numberOfButtons;
70+
71+
self.frame = CGRectMake(0.0f, 0.0f, buttonWidth, MIN_HEIGHT);
72+
}
73+
6674
- (void)setTitle:(NSString *)title forState:(UIControlState)state
6775
{
6876
[super setTitle:title forState:state];

0 commit comments

Comments
 (0)