iOS中默認樣式修改-b

項目中有大量的UITableView都須要顯示sectionHeader。iOS中默認sessionHeader上的textLabel樣式跟設計圖不符。

按照咱們以前的解決方案,是在每一個UITableviewController上註冊sessionHeader,而後在tableView:viewForHeaderInSection:方法中重用它們,而且修改文本字體和顏色。這種方式會產生必定量的重複代碼,而且不利於維護。誰知道客戶會不會心血來潮讓把文字改爲紅色……ios

在這裏咱們打算使用+ (instancetype)appearance;方法對全局的UITableViewHeaderFooterView樣式進行修改。首先嚐試的代碼是:session

[[UITableViewHeaderFooterView appearance].textLabel setFont:[UIFont systemFontOfSize:12]];

 

發現並無奏效,仔細閱讀了一下文檔發現這麼一段話app

To customize the appearance of all instances of a class, send the relevant appearance modification messages to the appearance proxy for the class.

 

字面上理解的應該是發送一個message到對象上,而上面的代碼是對於對象的一個屬性發送一個message。應該是這裏的問題。字體

繼續閱讀代碼,發現另外兩個方法ui

+ (instancetype)appearanceWhenContainedIn:(nullable Class <UIAppearanceContainer>)ContainerClass; + (instancetype)appearanceWhenContainedInInstancesOfClasses:(NSArray<Class <UIAppearanceContainer>> *)containerTypes;

 

 

這兩個方法分別對應ios9以前和以後 
實現以下:lua

#ifdef __IPHONE_9_0
    [[UILabel appearanceWhenContainedInInstancesOfClasses:@[[UITableViewHeaderFooterView class]]] setFont:[UIFont systemFontOfSize:12]]; #else [[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class]] setFont:[UIFont systemFontOfSize:12]]; #endif

 

 

問題解決~spa

相關文章
相關標籤/搜索