From 98aec096a959da6b4dffbda23a6795ed77466b49 Mon Sep 17 00:00:00 2001 From: Ric Santos Date: Fri, 17 Jun 2016 13:26:12 +0930 Subject: [PATCH] Allow alerts with 2 buttons to stack the buttons vertically --- NYAlertViewController/NYAlertView.h | 2 ++ NYAlertViewController/NYAlertView.m | 4 ++-- NYAlertViewController/NYAlertViewController.h | 7 +++++++ NYAlertViewController/NYAlertViewController.m | 7 +++++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/NYAlertViewController/NYAlertView.h b/NYAlertViewController/NYAlertView.h index f220c85..008b9ad 100644 --- a/NYAlertViewController/NYAlertView.h +++ b/NYAlertViewController/NYAlertView.h @@ -46,6 +46,8 @@ typedef NS_ENUM(NSInteger, NYAlertViewButtonType) { @property (nonatomic) CGFloat buttonCornerRadius; @property (nonatomic) CGFloat maximumWidth; +@property (nonatomic) BOOL alwaysStackButtonsVertically; + @property (nonatomic, readonly) UIView *alertBackgroundView; @property (nonatomic, readonly) NSLayoutConstraint *backgroundViewVerticalCenteringConstraint; diff --git a/NYAlertViewController/NYAlertView.m b/NYAlertViewController/NYAlertView.m index 227d52f..a0fb948 100644 --- a/NYAlertViewController/NYAlertView.m +++ b/NYAlertViewController/NYAlertView.m @@ -494,8 +494,8 @@ - (void)setActionButtons:(NSArray *)actionButtons { _actionButtons = actionButtons; - // If there are 2 actions, display the buttons next to each other. Otherwise, stack the buttons vertically at full width - if ([actionButtons count] == 2) { + // If there are 2 actions and always stack buttons is not enabled, display the buttons next to each other. Otherwise, stack the buttons vertically at full width + if ([actionButtons count] == 2 && !self.alwaysStackButtonsVertically) { UIButton *firstButton = actionButtons[0]; UIButton *lastButton = actionButtons[1]; diff --git a/NYAlertViewController/NYAlertViewController.h b/NYAlertViewController/NYAlertViewController.h index 88fb990..7c75d6c 100644 --- a/NYAlertViewController/NYAlertViewController.h +++ b/NYAlertViewController/NYAlertViewController.h @@ -72,6 +72,13 @@ typedef NS_ENUM(NSInteger, NYAlertViewControllerTransitionStyle) { */ @property (nonatomic) BOOL swipeDismissalGestureEnabled; +/** + A Boolean value that determines if the buttons should be stacked vertically. + + @discussion By default, two buttons are shown side by side, three or more are stacked. This will allow just two buttons to be stacked. + */ +@property (nonatomic) BOOL alwaysStackButtonsVertically; + /** The background color of the alert view */ diff --git a/NYAlertViewController/NYAlertViewController.m b/NYAlertViewController/NYAlertViewController.m index 7d3e8ab..0e17c33 100644 --- a/NYAlertViewController/NYAlertViewController.m +++ b/NYAlertViewController/NYAlertViewController.m @@ -350,6 +350,7 @@ - (void)commonInit { _textFields = [NSArray array]; _showsStatusBar = YES; + _alwaysStackButtonsVertically = NO; _buttonTitleFont = [UIFont systemFontOfSize:16.0f]; _cancelButtonTitleFont = [UIFont boldSystemFontOfSize:16.0f]; @@ -407,6 +408,12 @@ - (void)setSwipeDismissalGestureEnabled:(BOOL)swipeDismissalGestureEnabled { self.panGestureRecognizer.enabled = swipeDismissalGestureEnabled; } +- (void)setAlwaysStackButtonsVertically:(BOOL)alwaysStackButtonsVertically { + _alwaysStackButtonsVertically = alwaysStackButtonsVertically; + + self.view.alwaysStackButtonsVertically = alwaysStackButtonsVertically; +} + - (void)panGestureRecognized:(UIPanGestureRecognizer *)gestureRecognizer { self.view.backgroundViewVerticalCenteringConstraint.constant = [gestureRecognizer translationInView:self.view].y;