iOS开发--自定义UIAlertController
2015-10-13 11:20
573 查看
在日常开发中,我们往往要满足各种需求,自定义AlertController颜色等,也算其一,那么,如何来自定义系统控件呢?下面提供了一种自定义UIAlertController的方式:
Tips: 通过KVO,我们可以给AlertController设置自定义attributedTitle,和attributedMessag、还可以给按钮设置图片等;同时,我们还可以通过设置TintColor的方式来改变按钮颜色
参考: https://github.com/devSC/iOS-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UIAlertController.h
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开发之路--微博“更多”页面
- iOS开发之路--微博骨架搭建
- IOS开发代码分享之获取启动画面图片的string
- IOS开发之路--C语言数组和字符串
- IOS开发代码分享之用nstimer实现倒计时功能
- IOS开发代码分享之设置UISearchBar的背景颜色
- ios开发之环境搭建-01
- 在开发iOS程序时对日期处理的总结
- iOS开发——keychain的使用
- iOS Graphics Technologies iOS 图形技术
- iOS开发异常错误总结
- 学习Objective-C:入门手册
- iOS开发 简单实现视频音频的边下边播
- iOS开发 — Quartz 2D知识点应用 (制作了一个Demo,源代码)
- ios开发对配置文件plist的操作
- iphone5和4的屏幕兼容问题
- 9秒的课程只为培养靠谱的IOS和HTML5开发人才而定制
- iOS开发学习笔记 1-0 开篇
- iOS开发学习笔记 2-1 C语言部分 初识C语言
- iOS开发学习笔记 2-2 C语言部分 类型、运算符与表达式