Custom Alert

// // AlertView.m // Swish // // Created by Dan Merlea on 02/12/15. // Copyright © 2015 Dan Merlea. All rights reserved. // #import "AlertView.h" @interface AlertView () @property (strong, nonatomic) IBOutlet NSLayoutConstraint *constraintWidthConfirmButton; @property (strong, nonatomic) IBOutlet UIView *viewContainer; @property (strong, nonatomic) IBOutlet UIView *viewIcon; @property (strong, nonatomic) IBOutlet UIImageView *imageViewIcon; @property (strong, nonatomic) IBOutlet UIButton *buttonCancel; @property (strong, nonatomic) IBOutlet UIButton *buttonConfirm; @property (strong, nonatomic) IBOutlet UILabel *labelTitle; @property (strong, nonatomic) IBOutlet UILabel *labelDescription; @property (copy, nonatomic) void (^blockAction)(void); @end @implementation AlertView - (void)awakeFromNib { self.viewContainer.layer.cornerRadius = 20; self.viewContainer.layer.masksToBounds = YES; self.viewIcon.layer.cornerRadius = CGRectGetHeight(self.viewIcon.frame) / 2; self.viewIcon.layer.borderColor = [COLOR_F0F0F0 CGColor]; self.viewIcon.layer.borderWidth = 2.0; } + (AlertView *)alertWithIcon:(UIImage *)icon title:(NSString *)title description:(NSString *)description cancelButtonTitle:(NSString *)cancelButtonTitle confirmButtonTitle:(NSString *)confirmButtonTitle confirmBlock:(void(^)())confirmBlock { NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"AlertView" owner:self options:nil]; AlertView *alert = [views objectAtIndex:0]; alert.frame = [alert countScreenFrame]; alert.alpha = 0; if (confirmButtonTitle == nil) { alert.constraintWidthConfirmButton.constant = 0; [alert layoutIfNeeded]; } alert.imageViewIcon.image = icon; alert.labelTitle.text = title; alert.labelDescription.text = description; [alert.buttonCancel setTitle:cancelButtonTitle forState:UIControlStateNormal]; [alert.buttonConfirm setTitle:confirmButtonTitle forState:UIControlStateNormal]; alert.blockAction = confirmBlock; UIViewController *viewController = [self topViewController]; [viewController.view addSubview:alert]; [UIView animateWithDuration:0.3 animations:^{ alert.alpha = 1; } completion:nil]; return alert; } - (void)dismiss { [UIView animateWithDuration:0.3 animations:^{ self.alpha = 0; } completion:^(BOOL finished) { if (finished) { [self removeFromSuperview]; } }]; } - (IBAction)dismissAlert:(UIButton *)sender { if (self.dismissable) { [self dismiss]; } } - (IBAction)invokeBlock:(UIButton *)sender { self.blockAction(); } + (UIViewController*)topViewController { return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController]; } + (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController { if ([rootViewController isKindOfClass:[UITabBarController class]]) { UITabBarController* tabBarController = (UITabBarController*)rootViewController; return [self topViewControllerWithRootViewController:tabBarController.selectedViewController]; } else if ([rootViewController isKindOfClass:[UINavigationController class]]) { UINavigationController* navigationController = (UINavigationController*)rootViewController; return [self topViewControllerWithRootViewController:navigationController.visibleViewController]; } else if (rootViewController.presentedViewController) { UIViewController* presentedViewController = rootViewController.presentedViewController; return [self topViewControllerWithRootViewController:presentedViewController]; } else { return rootViewController; } } - (CGRect)countScreenFrame { CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width; CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height; // On iOS7, screen width and height doesn't automatically follow orientation if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) { UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation]; if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) { CGFloat tmp = screenWidth; screenWidth = screenHeight; screenHeight = tmp; } } return CGRectMake(0, 0, screenWidth, screenHeight); } @end
Create a custom alert over the view controller or on a specific view

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.