MKAlertController(修改系统样式)

MKAlertController

github: MKAlertController

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

效果图

image
image

添加

  • 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;
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,369评论 4 61
  • 如何发起同步和异步请求 OkHttp 的请求实际上由 RealCall 对象发起,RealCall 提供了 exe...
    石器时代小古董阅读 8,120评论 0 2
  • _index.js 由于按钮组件可能用到按钮组,因此多写了一个button-group组件。 Button Gro...
    liril阅读 31,424评论 0 7
  • 让人感到时光流逝,除了成人礼,就是理发了。后青春的成长,本来就一场生命的洗礼,无需太多的仪式感。诚然,头发给人带来...
    安言靜语阅读 2,701评论 0 0
  • 这是一张很简单的风景画,是想画出半江瑟瑟半江红的感觉。构图很简单。可惜最后小船在斯留白液的时候撕坏了画纸表面,小...
    天目心阅读 6,473评论 2 2

友情链接更多精彩内容