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 changing the background view #15

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
6 changes: 5 additions & 1 deletion NYAlertViewController/NYAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ typedef NS_ENUM(NSInteger, NYAlertViewButtonType) {
@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
50 changes: 32 additions & 18 deletions NYAlertViewController/NYAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,28 @@ @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,23 +300,25 @@ - (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;
if (self.flexibleWidth) {
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];
}

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

[self addConstraint:self.alertBackgroundWidthConstraint];

_backgroundViewVerticalCenteringConstraint = [NSLayoutConstraint constraintWithItem:self.alertBackgroundView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
Expand Down
16 changes: 16 additions & 0 deletions NYAlertViewController/NYAlertViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

#import <UIKit/UIKit.h>
#import "NYAlertView.h"

@interface NYAlertAction : NSObject

Expand Down Expand Up @@ -34,6 +35,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 Down Expand Up @@ -187,6 +198,11 @@ typedef NS_ENUM(NSInteger, NYAlertViewControllerTransitionStyle) {
*/
@property (nonatomic, readonly) NSArray *textFields;

/**
An instance of NYAlertView to represent the view of this controller
*/
@property (nonatomic, strong) NYAlertView *view;

/**
Add an alert action object to be displayed in the alert view

Expand Down
23 changes: 20 additions & 3 deletions NYAlertViewController/NYAlertViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

#import "NYAlertViewController.h"

#import "NYAlertView.h"

@interface NYAlertAction ()

@property (weak, nonatomic) UIButton *actionButton;
Expand Down Expand Up @@ -299,7 +297,6 @@ - (void)tapGestureRecognized:(UITapGestureRecognizer *)gestureRecognizer {

@interface NYAlertViewController () <UIGestureRecognizerDelegate, UIViewControllerTransitioningDelegate>

@property NYAlertView *view;
@property UIPanGestureRecognizer *panGestureRecognizer;
@property (nonatomic, strong) id<UIViewControllerTransitioningDelegate> transitioningDelegate;

Expand All @@ -319,6 +316,14 @@ + (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)m
return alertController;
}

+ (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message backgroundView:(UIView *)backgroundView{
NYAlertViewController *alertController = [[NYAlertViewController alloc] initWithNibName:nil bundle:nil backgroundView:backgroundView];
alertController.title = title;
alertController.message = message;

return alertController;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

Expand All @@ -329,6 +334,17 @@ - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibB
return self;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil backgroundView:(UIView *)backgroundView{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {
self.view = [[NYAlertView alloc] initWithFrame:backgroundView.frame backgroundView:backgroundView];
[self commonInit];
}

return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];

Expand Down Expand Up @@ -467,6 +483,7 @@ - (void)viewWillAppear:(BOOL)animated {
}

- (void)setAlertViewBackgroundColor:(UIColor *)alertViewBackgroundColor {

_alertViewBackgroundColor = alertViewBackgroundColor;

self.view.alertBackgroundView.backgroundColor = alertViewBackgroundColor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ - (void)showStandardAlertView {
[self dismissViewControllerAnimated:YES completion:nil];
}]];

alertController.view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"small-napkin"]];

[self presentViewController:alertController animated:YES completion:nil];
}

- (void)showCustomAlertViewWithActionCount:(NSInteger)actionCount {
NYAlertViewController *alertViewController = [[NYAlertViewController alloc] initWithNibName:nil bundle:nil];
NYAlertViewController *alertViewController = [[NYAlertViewController alloc] initWithNibName:nil bundle:nil backgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"small-napkin"]]];
alertViewController.view.flexibleWidth = NO;
alertViewController.title = NSLocalizedString(@"Example Title", nil);
alertViewController.message = NSLocalizedString(@"This alert uses the fade transition style! Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Donec id elit non mi porta gravida at eget metus.", nil);
alertViewController.message = NSLocalizedString(@"This alert uses the fade transition style!", nil);

alertViewController.view.tintColor = self.view.tintColor;
alertViewController.backgroundTapDismissalGestureEnabled = YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
}
],
"info" : {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "small-napkin.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.