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

Allow alerts with 2 buttons to stack the buttons vertically #24

Open
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NYAlertViewController/NYAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions NYAlertViewController/NYAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
7 changes: 7 additions & 0 deletions NYAlertViewController/NYAlertViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
7 changes: 7 additions & 0 deletions NYAlertViewController/NYAlertViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ - (void)commonInit {
_textFields = [NSArray array];

_showsStatusBar = YES;
_alwaysStackButtonsVertically = NO;

_buttonTitleFont = [UIFont systemFontOfSize:16.0f];
_cancelButtonTitleFont = [UIFont boldSystemFontOfSize:16.0f];
Expand Down Expand Up @@ -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;

Expand Down