JCAlertController
is same level
with UIAlertController
.
It is inherited
from UIViewController
too.
It supports managing presented controllers with FIFO
or LIFO
.
Compare | Present Queue | Custom Style | Custom View | Minimum iOS Target |
---|---|---|---|---|
JCAlertController | support | simple | simple | iOS 7 |
UIAlertController | not support | difficult | difficult | iOS 8 |
- support present queue
- support custom style
- support custom contentView
Look at the flowing code:
[self presentWithFIFO:alert1];
[self presentWithFIFO:alert2];
[self presentWithFIFO:alert3];
Result:
alert1 shows first
, after dismissed by user, alert2 shows second
, after dismissed by user, alert3 shows third
.
like this: alert1
>> alert2
>> alert3
[self presentWithLIFO:alert1];
[self presentWithLIFO:alert2];
[self presentWithLIFO:alert3];
Result:
alert3 shows first
, after dismissed by user, alert2 shows second
, after dismissed by user, alert1 shows third
.
like this alert3
>> alert2
>> alert1
.
|____.DS_Store
|____AlertView
| |____JCAlertView.h
| |____JCAlertView.m
|____ButtonItem
| |____JCAlertButtonItem.h
| |____JCAlertButtonItem.m
|____Category
| |____NSAttributedString+JCCalculateSize.h
| |____NSAttributedString+JCCalculateSize.m
| |____UIColor+JCHightlightedColor.h
| |____UIColor+JCHightlightedColor.m
| |____UIImage+JCColor2Image.h
| |____UIImage+JCColor2Image.m
| |____UIViewController+JCPresentQueue.h // present category
| |____UIViewController+JCPresentQueue.m
| |____UIWindow+JCBlur.h
| |____UIWindow+JCBlur.m
|____JCAlertController.h // import this
|____JCAlertController.m
|____Style
| |____JCAlertStyle.h
| |____JCAlertStyle.m
| |____JCAlertStyleAlertView.h
| |____JCAlertStyleAlertView.m
| |____JCAlertStyleBackground.h
| |____JCAlertStyleBackground.m
| |____JCAlertStyleButton.h
| |____JCAlertStyleButton.m
| |____JCAlertStyleButtonCancel.h
| |____JCAlertStyleButtonCancel.m
| |____JCAlertStyleButtonNormal.h
| |____JCAlertStyleButtonNormal.m
| |____JCAlertStyleButtonWarning.h
| |____JCAlertStyleButtonWarning.m
| |____JCAlertStyleContent.h
| |____JCAlertStyleContent.m
| |____JCAlertStyleSeparator.h
| |____JCAlertStyleSeparator.m
| |____JCAlertStyleTitle.h
| |____JCAlertStyleTitle.m
step 1
platform :ios, '7.0'
target 'your target' do
pod 'JCAlertController'
end
step 2
#import "JCAlertController.h"
title only
JCAlertController *alert = [JCAlertController alertWithTitle:@"I am title" message:nil type:JCAlertTypeTitleOnly];
[alert addButtonWithTitle:@"Cancel" type:JCButtonTypeCancel clicked:nil];
[alert addButtonWithTitle:@"OK" type:JCButtonTypeNormal clicked:nil];
[self jc_presentViewController:alert presentType:JCPresentTypeLIFO presentCompletion:nil dismissCompletion:nil];
content only
JCAlertController *alert = [JCAlertController alertWithTitle:nil message:@"I am content" type:JCAlertTypeContentOnly];
[alert addButtonWithTitle:@"OK" type:JCButtonTypeNormal clicked:nil];
[self jc_presentViewController:alert presentType:JCPresentTypeLIFO presentCompletion:nil dismissCompletion:nil];
title and content both
JCAlertController *alert = [JCAlertController alertWithTitle:@"I am title" message:@"I am content" type:JCAlertTypeNormal];
[alert addButtonWithTitle:@"OK" type:JCButtonTypeNormal clicked:nil];
[self jc_presentViewController:alert presentType:JCPresentTypeLIFO presentCompletion:nil dismissCompletion:nil];
custom style
// See all properties in JCAlertStyle
JCAlertStyle *style = [JCAlertStyle styleWithType:JCAlertTypeCustom];
style.background.blur = NO;
style.alertView.cornerRadius = 4;
style.title.backgroundColor = [UIColor colorWithRed:251/255.0 green:2/255.0 blue:19/255.0 alpha:1.0];
style.title.textColor = [UIColor whiteColor];
style.content.backgroundColor = [UIColor colorWithRed:251/255.0 green:2/255.0 blue:19/255.0 alpha:1.0];
style.content.textColor = [UIColor whiteColor];
style.content.insets = UIEdgeInsetsMake(20, 20, 40, 20);
style.buttonNormal.textColor = [UIColor colorWithRed:248/255.0 green:59/255.0 blue:50/255.0 alpha:1.0];
style.buttonNormal.highlightTextColor = [style.buttonNormal.textColor hightlightedColor];
style.buttonNormal.backgroundColor = [UIColor whiteColor];
style.buttonNormal.highlightBackgroundColor = [UIColor whiteColor];
JCAlertController *alert = [JCAlertController alertWithTitle:@"I am title" message:@"I am content" type:JCAlertTypeCustom];
[alert addButtonWithTitle:@"OK" type:JCButtonTypeNormal clicked:nil];
[self jc_presentViewController:alert presentType:JCPresentTypeLIFO presentCompletion:nil dismissCompletion:nil];
present with LIFO
// LIFO: alert3 >> alert2 >> alert4 >> alert1
// alert1
JCAlertController *alert1 = [JCAlertController alertWithTitle:@"alert1" message:nil type:JCAlertTypeTitleOnly];
[alert1 addButtonWithTitle:@"OK" type:JCButtonTypeNormal clicked:nil];
[self jc_presentViewController:alert1 presentType:JCPresentTypeLIFO presentCompletion:nil dismissCompletion:nil];
// alert2
JCAlertController *alert2 = [JCAlertController alertWithTitle:@"alert2" message:nil type:JCAlertTypeTitleOnly];
[alert2 addButtonWithTitle:@"OK" type:JCButtonTypeNormal clicked:^{
// alert4
JCAlertController *alert = [JCAlertController alertWithTitle:@"alert4" message:nil type:JCAlertTypeTitleOnly];
[alert addButtonWithTitle:@"OK" type:JCButtonTypeNormal clicked:nil];
[self jc_presentViewController:alert presentType:JCPresentTypeLIFO presentCompletion:nil dismissCompletion:nil];
}];
[self jc_presentViewController:alert2 presentType:JCPresentTypeLIFO presentCompletion:nil dismissCompletion:nil];
// alert3
JCAlertController *alert3 = [JCAlertController alertWithTitle:@"alert3" message:nil type:JCAlertTypeTitleOnly];
[alert3 addButtonWithTitle:@"OK" type:JCButtonTypeNormal clicked:nil];
[self jc_presentViewController:alert3 presentType:JCPresentTypeLIFO presentCompletion:nil dismissCompletion:nil];
present with FIFO
// FIFO >> alert2 >> alert3 >> alert4
// alert1
JCAlertController *alert1 = [JCAlertController alertWithTitle:@"alert1" message:nil type:JCAlertTypeTitleOnly];
[alert1 addButtonWithTitle:@"OK" type:JCButtonTypeNormal clicked:nil];
[self jc_presentViewController:alert1 presentType:JCPresentTypeFIFO presentCompletion:nil dismissCompletion:nil];
// alert2
JCAlertController *alert2 = [JCAlertController alertWithTitle:@"alert2" message:nil type:JCAlertTypeTitleOnly];
[alert2 addButtonWithTitle:@"OK" type:JCButtonTypeNormal clicked:^{
// alert4
JCAlertController *alert = [JCAlertController alertWithTitle:@"alert4" message:nil type:JCAlertTypeTitleOnly];
[alert addButtonWithTitle:@"OK" type:JCButtonTypeNormal clicked:nil];
[self jc_presentViewController:alert presentType:JCPresentTypeFIFO presentCompletion:nil dismissCompletion:nil];
}];
[self jc_presentViewController:alert2 presentType:JCPresentTypeFIFO presentCompletion:nil dismissCompletion:nil];
// alert3
JCAlertController *alert3 = [JCAlertController alertWithTitle:@"alert3" message:nil type:JCAlertTypeTitleOnly];
[alert3 addButtonWithTitle:@"OK" type:JCButtonTypeNormal clicked:nil];
[self jc_presentViewController:alert3 presentType:JCPresentTypeFIFO presentCompletion:nil dismissCompletion:nil];
custom contentView
UIImageView *contentView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 175)];
contentView.image = [UIImage imageNamed:@"alert"];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
[contentView addGestureRecognizer:tap];
contentView.userInteractionEnabled = YES;
JCAlertController *alert = [JCAlertController alertWithTitle:nil contentView:contentView type:JCAlertTypeNormal];
[self jc_presentViewController:alert presentType:JCPresentTypeLIFO presentCompletion:nil dismissCompletion:nil];
custom contentView and keyboard handle
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 240, 26)];
textField.backgroundColor = [UIColor colorWithRed:241/255.0 green:241/255.0 blue:241/255.0 alpha:1.0];
textField.layer.cornerRadius = 3;
textField.clipsToBounds = YES;
textField.center = CGPointMake(140, 30);
textField.secureTextEntry = YES;
textField.textAlignment = NSTextAlignmentCenter;
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 60)];
[contentView addSubview:textField];
JCAlertController *alert = [JCAlertController alertWithTitle:@"Enter password please" contentView:contentView type:JCAlertTypeNormal];
[alert addButtonWithTitle:@"Confirm" type:JCButtonTypeNormal clicked:^{
NSLog(@"You inputed:%@", textField.text);
[textField resignFirstResponder];
}];
[self jc_presentViewController:alert presentType:JCPresentTypeLIFO presentCompletion:^{
[textField becomeFirstResponder];
} dismissCompletion:nil];
// avoid retain circle
__weak typeof(JCAlertController *) weakalert = alert;
// callback after keyboard shows
[alert monitorKeyboardShowed:^(CGFloat alertHeight, CGFloat keyboardHeight) {
[weakalert moveAlertViewToCenterY:alertHeight / 2 + 120 animated:YES];
}];
// callback after keyboard hides
[alert monitorKeyboardHided:^{
[weakalert moveAlertViewToScreenCenterAnimated:YES];
}];
custom contentView and attributedstring
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 280, 100)];
label.textAlignment = NSTextAlignmentCenter;
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"Hello"];
[AttributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 2)];
[AttributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 3)];
label.attributedText = AttributedStr;
JCAlertController *alert = [JCAlertController alertWithTitle:nil contentView:label type:JCAlertTypeNormal];
[alert addButtonWithTitle:@"OK" type:JCButtonTypeNormal clicked:nil];
[self jc_presentViewController:alert presentType:JCPresentTypeLIFO presentCompletion:nil dismissCompletion:nil];
E-mail: [email protected]
Blog: http://www.jianshu.com/u/8bde69945e50