tintColor是iOS7在UIView中新加屬性尉尾,翻譯為中文是“著色”缘薛。如果未設(shè)置tintColor的顏色值泛粹,則會返回系統(tǒng)默認(rèn)設(shè)置了的藍色顏色值胰柑,這個值會影響以當(dāng)前視圖為根視圖的整個視圖層次結(jié)構(gòu)截亦。例如爬泥,在頁面中創(chuàng)建一個Button按鈕,我們發(fā)現(xiàn)按鈕的標(biāo)題顏色默認(rèn)為藍色崩瓤,默認(rèn)情況下一個視圖的tintColor是nil袍啡,這個視圖會使用父視圖的tintColor值。當(dāng)我們設(shè)置指定視圖的tintColor的屬性值后谷遂,這個著色值將會傳遞到在這個視圖的視圖層級的所有子視圖葬馋,如果子視圖沒有設(shè)置tintColor,則默認(rèn)使用父視圖的顏色值肾扰。
1畴嘶、測試代碼
//父視圖
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 120)];
[backView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:backView];
[backView setTintColor:[UIColor redColor]];
//在父視圖backView中添加第一個一個button1
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
[button1.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0f]]; [button1 setBackgroundColor:[UIColor yellowColor]];
[button1 setTitle:@"TestA" forState:UIControlStateNormal];
[backView addSubview:button1];
//在父視圖backView中添加第二個button2
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
[button2.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0f]];
[button2 setBackgroundColor:[UIColor yellowColor]];
[button2 setTitle:@"TestB" forState:UIControlStateNormal];
[backView addSubview:button2];
button2.tintColor = [UIColor greenColor];
//添加與backView同級的button到self.view上
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
[button3.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0f]];
[button3 setBackgroundColor:[UIColor yellowColor]];
[button3 setTitle:@"TestB" forState:UIControlStateNormal];
[self.view addSubview:button3];
- self.view的tintColor未設(shè)置顏色,則使用系統(tǒng)藍色默認(rèn)值集晚,button3為self.view的子視圖窗悯,由于未設(shè)置自身的著色值,根據(jù)tintColor傳遞使用原則button3的tintColor也是系統(tǒng)默認(rèn)值偷拔,標(biāo)題顯示藍色蒋院;
- backView是self.view的子視圖,button1和button2是backView的子視圖莲绰,此時backView的tintColor設(shè)置為
紅色
欺旧,button1的tintColor未設(shè)置,button2設(shè)置為綠色
蛤签,顯示結(jié)果為button1使用backView傳遞過來的著色值紅色
辞友,button2顯示自身設(shè)置的著色值綠色
。
4震肮、tintAdjustmentMode屬性
這個屬性是一個枚舉屬性称龙,它定義了tintColor的調(diào)整模式;
typedef NS_ENUM(NSInteger, UIViewTintAdjustmentMode) {
UIViewTintAdjustmentModeAutomatic,// 視圖的著色調(diào)整模式與父視圖一致
UIViewTintAdjustmentModeNormal,// 視圖的tintColor屬性返回完全未修改的視圖著色顏色
UIViewTintAdjustmentModeDimmed,//視圖的tintColor屬性返回一個去飽和度的、變暗的視圖著色顏色
}
下面是設(shè)置為Dimmed的效果
3戳晌、tintColorDidChange
這個方法會在視圖的tintColor或tintAdjustmentMode屬性改變時自動調(diào)用鲫尊。另外,如果當(dāng)前視圖的父視圖的tintColor或tintAdjustmentMode屬性改變時沦偎,也會調(diào)用這個方法疫向。我們可以在這個方法中根據(jù)需要去刷新我們的視圖。
4豪嚎、UIImageView使用tintColor
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 120)];
UIImage *image = [[UIImage imageNamed:@"tabbar_Image"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; imageView.contentMode = UIViewContentModeCenter;
imageView.tintColor = [UIColor redColor];
[imageView setImage:image];
[self.view addSubview:imageView];
左側(cè)為原圖搔驼,右側(cè)為設(shè)置tintColor為紅色的結(jié)果
在對圖片處理時,如果圖片的一個像素的alpha是1,則將它的顏色渲染為tintColor的值疙渣。如果alpha小于1的時候匙奴,則設(shè)置為透明,不會被渲染妄荔。這個處理比較暴力,若果圖片的顏色多樣的話并且alpha=1就會渲染為一種顏色泼菌。
- 介紹imageWithRenderingMode:
使用renderingMode設(shè)置UIImage的渲染模式谍肤,可以設(shè)定UIImage是否使用當(dāng)前視圖的tintColor渲染。
typedef NS_ENUM(NSInteger, UIImageRenderingMode) {
UIImageRenderingModeAutomatic, // 根據(jù)圖片的使用環(huán)境和所處的繪圖上下文自動調(diào)整渲染模式哗伯。
UIImageRenderingModeAlwaysOriginal, // 按照原圖進行渲染荒揣,始終繪制圖片原始狀態(tài),不使用Tint Color
UIImageRenderingModeAlwaysTemplate, // 始終根據(jù)TintColor繪制圖片焊刹,忽略圖片的顏色信息
}
5系任、APP設(shè)置全局TintColor
[[UIApplication sharedApplication] keyWindow].tintColor = [UIColor redColor];