MKAlertController
github: MKAlertController
利用OC的runtime 動(dòng)態(tài)修改系統(tǒng)自帶UIAlertController 的 category【貉可自定義樣式。
效果圖
![image](https://github.com/mk2016/MKAlertController/raw/master/Screenshots/domeGif.gif)
image
添加
cocoapods
pod 'MKAlertController', '~> 1.0.0'Manually (手動(dòng)導(dǎo)入)
只需將 MKAlertController 文件添加到項(xiàng)目中即可
用法 詳細(xì)用法參見(jiàn)demo
switch (indexPath.row) {
case 0:{
UIAlertController *ac = [UIAlertController mk_alertWithTitle:@"title" message:@"message" cancelTitle:@"取消" confirmTitle:@"確定" preferredStyle:UIAlertControllerStyleAlert block:^(NSInteger buttonIndex) {
NSLog(@"button index : %zd", buttonIndex);
}];
[self presentViewController:ac animated:YES completion:nil];
}
break;
case 1:{
UIAlertController *ac = [UIAlertController mk_alertWithTitle:@"this is a loooooooooooooong title" message:nil buttonTitles:@[@"button0",@"button1"] preferredStyle:UIAlertControllerStyleAlert block:^(NSInteger buttonIndex) {
NSLog(@"button index : %zd", buttonIndex);
}];
[self presentViewController:ac animated:YES completion:nil];
break;
}
case 2:{
MKAlertCtrlConfig *config = [[MKAlertCtrlConfig alloc] init];
config.titleFont = [UIFont systemFontOfSize:24];
config.titleColor = [UIColor grayColor];
config.titleAlignment = NSTextAlignmentCenter;
UIAlertController *ac = [UIAlertController mk_alertWithTitle:@"this is a loooooooooooooooooooong title \n& message is nil" message:nil cancelTitle:@"取消" confirmTitle:@"確認(rèn)" preferredStyle:UIAlertControllerStyleAlert config:config block:^(NSInteger buttonIndex) {
NSLog(@"button index : %zd", buttonIndex);
}];
[self presentViewController:ac animated:YES completion:nil];
break;
}
case 3:{
MKAlertCtrlConfig *config = [[MKAlertCtrlConfig alloc] init];
config.messageFont = [UIFont systemFontOfSize:14];
config.messageColor = [UIColor orangeColor];
config.messageAlignment = NSTextAlignmentLeft;
UIAlertController *ac = [UIAlertController mk_alertWithTitle:nil message:@"this is a message \n& title is nil \n& NSTextAlignmentLeft" preferredStyle:UIAlertControllerStyleAlert config:config block:^(NSInteger buttonIndex) {
NSLog(@"button index : %zd", buttonIndex);
} buttonTitles:@"button0",@"button1", nil];
[self presentViewController:ac animated:YES completion:nil];
break;
}
case 4:{
MKAlertCtrlConfig *config = [[MKAlertCtrlConfig alloc] init];
config.titleFont = [UIFont systemFontOfSize:30];
config.titleColor = [UIColor greenColor];
config.messageFont = [UIFont systemFontOfSize:12];
config.messageColor = [UIColor redColor];
config.messageAlignment = NSTextAlignmentLeft;
config.actionColor = [UIColor lightGrayColor];
config.destructivelColor = [UIColor purpleColor];
config.cancelIndex = 2;
config.destructiveIndex = 3;
UIAlertController *ac = [UIAlertController mk_alertWithTitle:@"title" message:@"message:\n1.xxxxxxx\n2.yyyyyyy\n3.zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" buttonTitles:@[@"button0",@"button1",@"cancel2",@"destructive3",@"button4"] preferredStyle:UIAlertControllerStyleAlert config:config block:^(NSInteger buttonIndex) {
NSLog(@"button index : %zd", buttonIndex);
}];
[self presentViewController:ac animated:YES completion:nil];
break;
}
case 5:{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"message:\n1.xxxxxxx\n2.yyyyyyy\n3.zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"action0" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"0");
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"action1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"1");
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"action2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"2");
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"action3" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"3");
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"action4" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"4");
}]];
MKAlertCtrlConfig *config = [[MKAlertCtrlConfig alloc] init];
config.titleFont = [UIFont systemFontOfSize:30];
config.titleColor = [UIColor greenColor];
config.messageFont = [UIFont systemFontOfSize:12];
config.messageColor = [UIColor redColor];
config.messageAlignment = NSTextAlignmentLeft;
config.actionColor = [UIColor lightGrayColor];
config.destructivelColor = [UIColor purpleColor];
config.destructiveIndex = 3;
[alertController mk_setCustomStyleWith:config];
// [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
// textField.font = [UIFont systemFontOfSize:30];
// textField.placeholder = @"xxx";
// }];
// [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
// textField.font = [UIFont systemFontOfSize:30];
// textField.placeholder = @"222";
// }];
[self presentViewController:alertController animated:YES completion:nil];
break;
}
case 6:{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"message:\n1.xxxxxxx\n2.yyyyyyy\n3.zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" preferredStyle:UIAlertControllerStyleActionSheet];
[alertController addAction:[UIAlertAction actionWithTitle:@"action0" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"0");
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"action1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"1");
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"action2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"2");
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"action3" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"3");
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"4");
}]];
MKAlertCtrlConfig *config = [[MKAlertCtrlConfig alloc] init];
config.titleFont = [UIFont systemFontOfSize:30];
config.titleColor = [UIColor greenColor];
config.messageFont = [UIFont systemFontOfSize:12];
config.messageColor = [UIColor redColor];
config.messageAlignment = NSTextAlignmentLeft;
config.actionColor = [UIColor lightGrayColor];
config.destructivelColor = [UIColor purpleColor];
config.destructiveIndex = 3;
[alertController mk_setCustomStyleWith:config];
[self presentViewController:alertController animated:YES completion:nil];
}
default:
break;
}