苦逼的開發者,最終敗給了一個任性的UI,系統原生UIAlertController的按紐顏色必須改.於是,開始了不歸路.
之前的版本是自己用view寫的一個仿系統UIActionSheet,動畫感覺都挺好,就是毛玻璃背景沒有系統的好,由於最低兼容了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@interface SCAlertAction : UIAlertAction @property (nonatomic,strong) UIColor *textColor; /**< 按鈕title字體顏色 */ @end @interface SCAlertController : UIAlertController @property (nonatomic,strong) UIColor *tintColor; /**< 統一按鈕樣式 不寫系統默認的藍色 */ @property (nonatomic,strong) UIColor *titleColor; /**< 標題的顏色 */ @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@implementation SCAlertAction //按鈕標題的字體顏色 -(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]; //標題顏色 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"]; } } //按鈕統一顏色 if (self.tintColor) { for (SCAlertAction *action in self.actions) { if (!action.textColor) { action.textColor = self.tintColor; } } } } @end
一般來說對於SCAlertController裡面的屬性應該像SCAlertAction一樣放在setter方法裡面修改,這裡我表示為了方便就放在這個方法裡面了.
-(void)viewDidLoad { [super viewDidLoad]; }
用法就很簡單了,和系統原生UIAlertController一樣,只是把UI換成SC,當然你可以改成自己喜歡的,但是別忘了改完.
SCAlertController *alertController = [SCAlertController alertControllerWithTitle:@"你確定要退出嗎?" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; alertController.tintColor = [UIColor redColor]; //這裡統一設置各個按鈕的顏色都為紅色. 當然,你還可以自定義某一個按鈕的顏色.比如下面的取消按鈕 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]; }