iOS13和以前的版本相比景埃,UISegmentedControl
的setTitleTextAttributes
函數(shù)的內(nèi)部實現(xiàn)方式發(fā)生了變化塑煎。
以代碼為例:
- (void)setNeedNavigationBarWithSegment: (NSArray<NSString *> *)titleArray {
...
UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:titleArray];
[segment setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected];
[segment setTitleTextAttributes:@{NSForegroundColorAttributeName:[Color standardBlue]} forState:UIControlStateNormal];
...
}
我們在基類控制器中自行繪制頭部視圖伟姐,上述函數(shù)是一個快速構(gòu)造頭部帶有UISegmentedControl
的頭部視圖师抄,我們在快速構(gòu)造函數(shù)中配置了segment
的選中文本顏色和普通狀態(tài)文本顏色仙逻。
然后在一個特殊的控制器中我們需要配置segemnt
的字體大小小于我們默認(rèn)的字體大小服鹅。
if ([self.titleView isKindOfClass:[UISegmentedControl class]]) {
UIFont *font = [UIFont systemFontOfSize:12];
NSMutableDictionary *normalAttributes = [NSMutableDictionary dictionary];
normalAttributes[NSFontAttributeName] = font;
[((UISegmentedControl *)self.titleView) setTitleTextAttributes:normalAttributes forState:UIControlStateNormal];
}
在iOS13以下的版本通過setTitleTextAttributes
函數(shù)配置相關(guān)屬性時涵但,上述代碼對字體大小的修改能與更上面對字體顏色的修改共存杈绸。
而在iOS13中,setTitleTextAttributes
則會覆蓋掉字體顏色矮瘟,使得Segment
會顯示默認(rèn)的黑色文本瞳脓。
于是就需要
UIFont *font = [UIFont systemFontOfSize:12];
NSMutableDictionary *normalAttributes = [NSMutableDictionary dictionary];
NSMutableDictionary *selectedAttributes = [NSMutableDictionary dictionary];
normalAttributes[NSFontAttributeName] = font;
normalAttributes[NSForegroundColorAttributeName] = [Color standardBlue];
selectedAttributes[NSFontAttributeName] = font;
selectedAttributes[NSForegroundColorAttributeName] = [UIColor whiteColor];
[((UISegmentedControl *)self.titleView) setTitleTextAttributes:normalAttributes forState:UIControlStateNormal];
[((UISegmentedControl *)self.titleView) setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
重新配置所有屬性,或者:
[((UISegmentedControl *)self.titleView) titleTextAttributesForState:<#(UIControlState)#>]
在配置參數(shù)前預(yù)先獲取已有的參數(shù)字典澈侠。
聯(lián)想到iOS13蘋果官方對UISegmentedControl
的大動作修改劫侧,合理地通過Demo進(jìn)行聯(lián)想,setTitleTextAttributes
的內(nèi)部實現(xiàn)已從原來的合并變成了覆蓋埋涧。
以上板辽。