Skip to content
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
21 changes: 21 additions & 0 deletions NYAlertViewController/NYAlertAction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// NYAlertAction.h
// NYAlertViewDemo
//
// Created by Guillermo Saenz on 6/14/16.
// Copyright © 2016 Nealon Young. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface NYAlertAction : NSObject

+ (instancetype)actionWithTitle:(NSString *)title style:(UIAlertActionStyle)style handler:(void (^)(NYAlertAction *action))handler;

@property (weak, nonatomic) UIButton *actionButton;
@property (nonatomic) NSString *title;
@property (nonatomic) UIAlertActionStyle style;
@property (nonatomic, strong) void (^handler)(NYAlertAction *action);
@property (nonatomic) BOOL enabled;

@end
38 changes: 38 additions & 0 deletions NYAlertViewController/NYAlertAction.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// NYAlertAction.m
// NYAlertViewDemo
//
// Created by Guillermo Saenz on 6/14/16.
// Copyright © 2016 Nealon Young. All rights reserved.
//

#import "NYAlertAction.h"

@implementation NYAlertAction

+ (instancetype)actionWithTitle:(NSString *)title style:(UIAlertActionStyle)style handler:(void (^)(NYAlertAction *action))handler {
NYAlertAction *action = [[NYAlertAction alloc] init];
action.title = title;
action.style = style;
action.handler = handler;

return action;
}

- (instancetype)init {
self = [super init];

if (self) {
_enabled = YES;
}

return self;
}

- (void)setEnabled:(BOOL)enabled {
_enabled = enabled;

self.actionButton.enabled = enabled;
}

@end
7 changes: 6 additions & 1 deletion NYAlertViewController/NYAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@ typedef NS_ENUM(NSInteger, NYAlertViewButtonType) {
@property (nonatomic) UIColor *destructiveButtonColor;
@property (nonatomic) UIColor *destructiveButtonTitleColor;

@property (nonatomic) BOOL showButtonsAlwaysVertically;
@property (nonatomic) CGFloat buttonCornerRadius;
@property (nonatomic) CGFloat maximumWidth;

@property (nonatomic, readonly) UIView *alertBackgroundView;
@property (nonatomic, strong) UIView *alertBackgroundView;

@property (nonatomic, readonly) NSLayoutConstraint *backgroundViewVerticalCenteringConstraint;

@property (nonatomic, assign) BOOL flexibleWidth;

- (instancetype)initWithFrame:(CGRect)frame backgroundView:(UIView *)backgroundView;

//@property (nonatomic) NSArray *actions;
@property (nonatomic) NSArray *actionButtons;

Expand Down
66 changes: 44 additions & 22 deletions NYAlertViewController/NYAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,27 @@ @interface NYAlertView ()

@implementation NYAlertView

- (instancetype)initWithFrame:(CGRect)frame backgroundView:(UIView *)backgroundView{
self = [super initWithFrame:frame];
if (self) {
self.alertBackgroundView = backgroundView;
self = [self initWithFrame:frame];
}
return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];

if (self) {
self.maximumWidth = 480.0f;

_alertBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
if (!_alertBackgroundView) {
_alertBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
self.alertBackgroundView.backgroundColor = [UIColor colorWithWhite:0.97f alpha:1.0f];
self.alertBackgroundView.layer.cornerRadius = 6.0f;
}
[self.alertBackgroundView setTranslatesAutoresizingMaskIntoConstraints:NO];
self.alertBackgroundView.backgroundColor = [UIColor colorWithWhite:0.97f alpha:1.0f];
self.alertBackgroundView.layer.cornerRadius = 6.0f;
[self addSubview:_alertBackgroundView];

_titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
Expand Down Expand Up @@ -288,22 +299,7 @@ - (instancetype)initWithFrame:(CGRect)frame {
multiplier:1.0f
constant:0.0f]];

CGFloat alertBackgroundViewWidth = MIN(CGRectGetWidth([UIApplication sharedApplication].keyWindow.bounds),
CGRectGetHeight([UIApplication sharedApplication].keyWindow.bounds)) * 0.8f;

if (alertBackgroundViewWidth > self.maximumWidth) {
alertBackgroundViewWidth = self.maximumWidth;
}

_alertBackgroundWidthConstraint = [NSLayoutConstraint constraintWithItem:self.alertBackgroundView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0f
constant:alertBackgroundViewWidth];

[self addConstraint:self.alertBackgroundWidthConstraint];
[self setFlexibleWidth:NO];

_backgroundViewVerticalCenteringConstraint = [NSLayoutConstraint constraintWithItem:self.alertBackgroundView
attribute:NSLayoutAttributeCenterY
Expand Down Expand Up @@ -361,7 +357,6 @@ - (instancetype)initWithFrame:(CGRect)frame {
return self;
}


// Pass through touches outside the backgroundView for the presentation controller to handle dismissal
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
for (UIView *subview in self.subviews) {
Expand All @@ -373,6 +368,33 @@ - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
return NO;
}

- (void)setFlexibleWidth:(BOOL)flexibleWidth {
if (flexibleWidth) {
if (_alertBackgroundWidthConstraint) {
[self removeConstraint:_alertBackgroundWidthConstraint];

_alertBackgroundWidthConstraint= NULL;
}
}else{
CGFloat alertBackgroundViewWidth = MIN(CGRectGetWidth([UIApplication sharedApplication].keyWindow.bounds),
CGRectGetHeight([UIApplication sharedApplication].keyWindow.bounds)) * 0.8f;

if (alertBackgroundViewWidth > self.maximumWidth) {
alertBackgroundViewWidth = self.maximumWidth;
}

_alertBackgroundWidthConstraint = [NSLayoutConstraint constraintWithItem:self.alertBackgroundView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0f
constant:alertBackgroundViewWidth];

[self addConstraint:self.alertBackgroundWidthConstraint];
}
}

- (void)setMaximumWidth:(CGFloat)maximumWidth {
_maximumWidth = maximumWidth;
self.alertBackgroundWidthConstraint.constant = maximumWidth;
Expand Down Expand Up @@ -494,8 +516,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 showsButtonsAlwaysVertically is set to NO, display the buttons next to each other. Otherwise, stack the buttons vertically at full width
if ([actionButtons count] == 2 && !self.showButtonsAlwaysVertically) {
UIButton *firstButton = actionButtons[0];
UIButton *lastButton = actionButtons[1];

Expand Down
35 changes: 25 additions & 10 deletions NYAlertViewController/NYAlertViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@

#import <UIKit/UIKit.h>

@interface NYAlertAction : NSObject

+ (instancetype)actionWithTitle:(NSString *)title style:(UIAlertActionStyle)style handler:(void (^)(NYAlertAction *action))handler;

@property (nonatomic) NSString *title;
@property (nonatomic) UIAlertActionStyle style;
@property (nonatomic, strong) void (^handler)(NYAlertAction *action);
@property (nonatomic) BOOL enabled;

@end
#import "NYAlertAction.h"

typedef NS_ENUM(NSInteger, NYAlertViewControllerTransitionStyle) {
/** Fade in the alert view */
Expand All @@ -34,6 +25,16 @@ typedef NS_ENUM(NSInteger, NYAlertViewControllerTransitionStyle) {
*/
+ (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message;

/**
Creates an alert view controller with the specified backgroundView
*/
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil backgroundView:(UIView *)backgroundView;

/**
Creates an alert view controller with the specified title ,message, and backgroundView
*/
+ (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message backgroundView:(UIView *)backgroundView;

/**
The message displayed under the alert view's title
*/
Expand All @@ -44,6 +45,16 @@ typedef NS_ENUM(NSInteger, NYAlertViewControllerTransitionStyle) {
*/
@property (nonatomic) BOOL showsStatusBar;

/**
A Boolean value that determines whether the buttons should be stacked always vertically even if there are just two buttons
*/
@property (nonatomic) BOOL showButtonsAlwaysVertically;

/**
A Boolean value that determines whether the width of the alert should be flexible or not
*/
@property (nonatomic) BOOL flexibleWidth;

/**
The custom view displayed in the presented alert view

Expand Down Expand Up @@ -72,6 +83,10 @@ typedef NS_ENUM(NSInteger, NYAlertViewControllerTransitionStyle) {
*/
@property (nonatomic) BOOL swipeDismissalGestureEnabled;

@property (nonatomic) UIColor *dimViewColor;

@property (nonatomic) CGFloat dimViewAlpha;

/**
The background color of the alert view
*/
Expand Down
Loading