您的位置:首页 > 产品设计 > UI/UE

iOS开发--自定义UIAlertController

2015-10-13 11:20 573 查看
在日常开发中,我们往往要满足各种需求,自定义AlertController颜色等,也算其一,那么,如何来自定义系统控件呢?下面提供了一种自定义UIAlertController的方式:

Tips: 通过KVO,我们可以给AlertController设置自定义attributedTitle,和attributedMessag、还可以给按钮设置图片等;同时,我们还可以通过设置TintColor的方式来改变按钮颜色

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Test" message:@"" preferredStyle:UIAlertControllerStyleAlert];
alert.view.tintColor = [UIColor redColor];

UIAlertAction *action = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];
//Add image 这里可以给button添加图片
UIImage *accessoryImage = [UIImage imageNamed:@"Icon_60"];
accessoryImage = [accessoryImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[action setValue:accessoryImage forKey:@"image"];

[alert addAction:action];

UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];
[alert addAction:action2];

//Custom Title
NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... Hulk Hogan!"];
[hogan addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:20]
range:NSMakeRange(24, 11)];
[alert setValue:hogan forKey:@"attributedTitle"];
[self presentViewController:alert animated:true completion:^{

}];
alert.view.tintColor = [UIColor orangeColor]; //修复IOS9 tintColor无法修改的问题

break;


参考: https://github.com/devSC/iOS-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UIAlertController.h
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios开发