事情是這樣的:設計稿給下來,系統原生 UIAlertController 的按鈕顏色必須改。於是開始了一段不歸路。
更早的版本是我自己用 view 手刻的一個仿系統 UIActionSheet,動畫手感都不錯,唯一輸在毛玻璃背景沒有系統的細緻。專案最低相容 iOS 8,所以就果斷放棄自建方案,改用 UIAlertController。
做法其實不難,核心就是 Runtime 機制。如果你對 Runtime 完全陌生,我的建議是別急著啃各種理論長文,找一個簡單 demo 多寫幾遍,自然就懂了。
思路是寫一個繼承自 UIAlertController 的子類,透過 KVC 去改它私有的 _attributedTitle、_attributedMessage 與按鈕的 _titleTextColor。
#import <UIKit/UIKit.h>
@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
#import "SCAlertController.h"
#import <objc/runtime.h>
@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 裡處理。這裡為了程式碼精簡,就先全部塞在 viewDidLoad 中了。
用法與系統原生的 UIAlertController 幾乎一致,只要把類別換成 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];
整體看下來,透過 Runtime 改私有 ivar 的做法雖然依賴了系統實作細節,但換來的是極低的接入成本——專案裡只要把原本的 UIAlertController 換成這個子類,視覺就能完全對齊設計稿。