diff --git a/NYAlertViewController/NYAlertView.h b/NYAlertViewController/NYAlertView.h index f220c85..81b9c1b 100644 --- a/NYAlertViewController/NYAlertView.h +++ b/NYAlertViewController/NYAlertView.h @@ -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; diff --git a/NYAlertViewController/NYAlertView.m b/NYAlertViewController/NYAlertView.m index 227d52f..b7037aa 100644 --- a/NYAlertViewController/NYAlertView.m +++ b/NYAlertViewController/NYAlertView.m @@ -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]; @@ -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 diff --git a/NYAlertViewController/NYAlertViewController.h b/NYAlertViewController/NYAlertViewController.h index 88fb990..5748e1a 100644 --- a/NYAlertViewController/NYAlertViewController.h +++ b/NYAlertViewController/NYAlertViewController.h @@ -6,6 +6,7 @@ // #import +#import "NYAlertView.h" @interface NYAlertAction : NSObject @@ -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 */ @@ -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 diff --git a/NYAlertViewController/NYAlertViewController.m b/NYAlertViewController/NYAlertViewController.m index 7d3e8ab..4c7909b 100644 --- a/NYAlertViewController/NYAlertViewController.m +++ b/NYAlertViewController/NYAlertViewController.m @@ -7,8 +7,6 @@ #import "NYAlertViewController.h" -#import "NYAlertView.h" - @interface NYAlertAction () @property (weak, nonatomic) UIButton *actionButton; @@ -299,7 +297,6 @@ - (void)tapGestureRecognized:(UITapGestureRecognizer *)gestureRecognizer { @interface NYAlertViewController () -@property NYAlertView *view; @property UIPanGestureRecognizer *panGestureRecognizer; @property (nonatomic, strong) id transitioningDelegate; @@ -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]; @@ -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]; @@ -467,6 +483,7 @@ - (void)viewWillAppear:(BOOL)animated { } - (void)setAlertViewBackgroundColor:(UIColor *)alertViewBackgroundColor { + _alertViewBackgroundColor = alertViewBackgroundColor; self.view.alertBackgroundView.backgroundColor = alertViewBackgroundColor; diff --git a/NYAlertViewControllerDemo/NYAlertViewDemo/DemoViewController.m b/NYAlertViewControllerDemo/NYAlertViewDemo/DemoViewController.m index d0d317b..1f7ab6f 100644 --- a/NYAlertViewControllerDemo/NYAlertViewDemo/DemoViewController.m +++ b/NYAlertViewControllerDemo/NYAlertViewDemo/DemoViewController.m @@ -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; diff --git a/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/AppIcon.appiconset/Contents.json b/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/AppIcon.appiconset/Contents.json index 36d2c80..eeea76c 100644 --- a/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/AppIcon.appiconset/Contents.json +++ b/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/AppIcon.appiconset/Contents.json @@ -59,6 +59,11 @@ "idiom" : "ipad", "size" : "76x76", "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "83.5x83.5", + "scale" : "2x" } ], "info" : { diff --git a/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/Contents.json b/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/Contents.json new file mode 100644 index 0000000..da4a164 --- /dev/null +++ b/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/small-napkin.imageset/Contents.json b/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/small-napkin.imageset/Contents.json new file mode 100644 index 0000000..6a8d473 --- /dev/null +++ b/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/small-napkin.imageset/Contents.json @@ -0,0 +1,23 @@ +{ + "images" : [ + { + "idiom" : "universal", + "filename" : "small-napkin.png", + "scale" : "1x" + }, + { + "idiom" : "universal", + "filename" : "small-napkin@2x.png", + "scale" : "2x" + }, + { + "idiom" : "universal", + "filename" : "small-napkin@3x.png", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/small-napkin.imageset/small-napkin.png b/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/small-napkin.imageset/small-napkin.png new file mode 100644 index 0000000..2877499 Binary files /dev/null and b/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/small-napkin.imageset/small-napkin.png differ diff --git a/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/small-napkin.imageset/small-napkin@2x.png b/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/small-napkin.imageset/small-napkin@2x.png new file mode 100644 index 0000000..4d111d2 Binary files /dev/null and b/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/small-napkin.imageset/small-napkin@2x.png differ diff --git a/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/small-napkin.imageset/small-napkin@3x.png b/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/small-napkin.imageset/small-napkin@3x.png new file mode 100644 index 0000000..53dd0d9 Binary files /dev/null and b/NYAlertViewControllerDemo/NYAlertViewDemo/Images.xcassets/small-napkin.imageset/small-napkin@3x.png differ