苦逼的開發(fā)者,最終敗給了一個任性的UI,系統(tǒng)原生UIAlertController的按紐顏色必須改.于是,開始了不歸路.
之前的版本是自己用view寫的一個仿系統(tǒng)UIActionSheet,動畫感覺都挺好,就是毛玻璃背景沒有系統(tǒng)的好,由于最低兼容了ios8,所以就拋棄了UIActionSheet,改用UIAlertController.
做法其實很簡單,采用runtime機制.對于runtime不了解的,我想還是別看各種介紹文章了,找一個簡單的demo多寫幾遍,就行了.
做法很簡單,自己寫一個類 繼承自UIAlertController,還是先把.h和.m的代碼都給大家吧.
SCAlertController.h
//
// SCAlertController.h
// SCAlertController
//
// Created by it3部01 on 16/8/3.
// Copyright ? 2016年 benben. All rights reserved.
//
#import <uikit uikit.h="">
@interface SCAlertAction : UIAlertAction
@property (nonatomic,strong) UIColor *textColor; /**< 按鈕title字體顏色 */
@end
@interface SCAlertController : UIAlertController
@property (nonatomic,strong) UIColor *tintColor; /**< 統(tǒng)一按鈕樣式 不寫系統(tǒng)默認(rèn)的藍色 */
@property (nonatomic,strong) UIColor *titleColor; /**< 標(biāo)題的顏色 */
@property (nonatomic,strong) UIColor *messageColor; /**< 信息的顏色 */
@end
SCAlertController.m
皇钞、综看、、
// SCAlertController.m
// SCAlertController
//
// Created by it3部01 on 16/8/3.
// Copyright ? 2016年 benben. All rights reserved.
//
#import "SCAlertController.h"
#import <objc runtime.h="">
@implementation SCAlertAction
//按鈕標(biāo)題的字體顏色
-(void)setTextColor:(UIColor *)textColor
{
_textColor = textColor;
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([UIAlertAction class], &count);
for(int i =0;i < count;i ++){
Ivar ivar = ivars[i];
NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];
if ([ivarName isEqualToString:@"_titleTextColor"]) {
[self setValue:textColor forKey:@"titleTextColor"];
}
}
}
@end
@implementation SCAlertController
-(void)viewDidLoad
{
[super viewDidLoad];
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([UIAlertController class], &count);
for(int i = 0;i < count;i ++){
Ivar ivar = ivars[i];
NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];
//標(biāo)題顏色
if ([ivarName isEqualToString:@"_attributedTitle"] && self.title && self.titleColor) {
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:self.title attributes:@{NSForegroundColorAttributeName:self.titleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:14.0]}];
[self setValue:attr forKey:@"attributedTitle"];
}
//描述顏色
if ([ivarName isEqualToString:@"_attributedMessage"] && self.message && self.messageColor) {
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:self.message attributes:@{NSForegroundColorAttributeName:self.messageColor,NSFontAttributeName:[UIFont systemFontOfSize:14.0]}];
[self setValue:attr forKey:@"attributedMessage"];
}
}
//按鈕統(tǒng)一顏色
if (self.tintColor) {
for (SCAlertAction *action in self.actions) {
if (!action.textColor) {
action.textColor = self.tintColor;
}
}
}
}
@end
使用
SCAlertController *alertController = [SCAlertController alertControllerWithTitle:@"你確定要退出嗎?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
alertController.tintColor = [UIColor redColor]; //這里統(tǒng)一設(shè)置各個按鈕的顏色都為紅色.
當(dāng)然,你還可以自定義某一個按鈕的顏色.比如下面的取消按鈕
alertController.titleColor = [UIColor redColor];
//取消
SCAlertAction *cancelAction = [SCAlertAction actionWithTitle:@"我不想退出" style:UIAlertActionStyleCancel handler:nil];
//單獨修改一個按鈕的顏色
cancelAction.textColor = [UIColor greenColor];
[alertController addAction:cancelAction];
//退出
SCAlertAction *exitAction = [SCAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:exitAction];
[self presentViewController:alertController animated:YES completion:nil];
}