美文网首页
MKAlertController(修改系统样式)

MKAlertController(修改系统样式)

作者: 答案MK | 来源:发表于2017-07-10 14:27 被阅读18次

MKAlertController

github: MKAlertController

利用OC的runtime 动态修改系统自带UIAlertController 的 category。可自定义样式。

效果图

imageimage

添加

  • cocoapods
    pod 'MKAlertController', '~> 1.0.0'

  • Manually (手动导入)
    只需将 MKAlertController 文件添加到项目中即可

用法 详细用法参见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:@"确认" 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;
    }

相关文章

网友评论

      本文标题:MKAlertController(修改系统样式)

      本文链接:https://www.haomeiwen.com/subject/dxnyhxtx.html