Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

带你玩转 UIAlertController

$
0
0

UIAlertController 是 iOS 8 中推出的新特性,用以代替 UIAlertView 和 UIActionSheet。在开发中非常常见,今天作一下简要记录,用以备忘。

之所以要把它单独拿出来说,是因为面对设计给出的 UI 界面需求,系统自身的默认配置是不能够得以满足的,常常需要 DIY 其属性。下面主要从设置字体大小、颜色、显示顺序以及弹出输入文本框等方面加以说明,也算得上是使用 UIAlertController 的一点点小小的总结。

1. 修改标题颜色字体大小

NSString *alertTitle = @"确定要关闭窗口吗";
NSMutableAttributedString *attTitle = [[NSMutableAttributedString alloc]initWithString:@"标题1" attributes:@{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:[UIFont systemFontOfSize:24]}];

UIAlertController *alert = [UIAlertController alertControllerWithTitle:alertTitle message:nil preferredStyle:UIAlertControllerStyleAlert];
[alert setValue:attTitle forKey:@"attributedTitle"];

NSString *okText = @"确定";
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:okText style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    }];
[confirmAction setValue:[UIColor blackColor] forKey:@"titleTextColor"];

NSString *cancelText = @"否";
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelText style:UIAlertActionStyleCancel handler:nil];
[cancelAction setValue:[UIColor redColor] forKey:@"titleTextColor"];

[alert addAction:confirmAction];
[alert addAction:cancelAction];

[self presentViewController:alert animated:NO completion:nil];

运行效果见文章末尾的图左边第一个。核心代码:

NSMutableAttributedString *attTitle = [[NSMutableAttributedString alloc]initWithString:@"标题1" attributes:@{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:[UIFont systemFontOfSize:24]}];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:alertTitle message:nil preferredStyle:UIAlertControllerStyleAlert];
[alert setValue:attTitle forKey:@"attributedTitle"];

2. 单独修改消息按钮颜色

NSString *alertTitle = @"确定要关闭窗口吗";

UIAlertController *alert = [UIAlertController alertControllerWithTitle:alertTitle message:nil preferredStyle:UIAlertControllerStyleAlert];

NSString *okText = @"确定";
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:okText style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    }];
[confirmAction setValue:[UIColor blackColor] forKey:@"titleTextColor"];

NSString *cancelText = @"否";
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelText style:UIAlertActionStyleCancel handler:nil];
[cancelAction setValue:[UIColor redColor] forKey:@"titleTextColor"];

[alert addAction:confirmAction];
[alert addAction:cancelAction];

[self presentViewController:alert animated:NO completion:nil];

运行效果见文章末尾的图左边第二个。核心代码(同样是 KVC):

[confirmAction setValue:[UIColor blackColor] forKey:@"titleTextColor"];
[cancelAction setValue:[UIColor redColor] forKey:@"titleTextColor"];

3. 改变消息按钮位置

要想改变消息按钮的位置,只需将两者的 style 都改为 UIAlertActionStyleDefault 即可。代码如下:

NSString *alertTitle = @"确定要关闭窗口吗";

UIAlertController *alert = [UIAlertController alertControllerWithTitle:alertTitle message:nil preferredStyle:UIAlertControllerStyleAlert];

NSString *okText = @"确定";
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:okText style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    }];
[confirmAction setValue:[UIColor blackColor] forKey:@"titleTextColor"];

NSString *cancelText = @"否";
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelText style:UIAlertActionStyleDefault handler:nil];
[cancelAction setValue:[UIColor redColor] forKey:@"titleTextColor"];

[alert addAction:confirmAction];
[alert addAction:cancelAction];

[self presentViewController:alert animated:NO completion:nil];

4. 弹出文本输入框

很多时候也有弹出输入框的需求,废话不多说,直接上代码:

UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"登陆"
                                                                      message: @"输入用户名密码"
                                                                       preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"name";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
    }];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"password";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.secureTextEntry = YES;
    }];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

    }]];
[alertController addAction:[UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    }]];

[self presentViewController:alertController animated:YES completion:nil];

最后,把上面四个实例的运行结果贴在末尾,供参考:
这里写图片描述

作者:huangfei711 发表于2017/8/22 11:04:13 原文链接
阅读:68 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>