學(xué)習(xí)使用
tintColor
屬性
參考
tintColor 屬性
@property(null_resettable, nonatomic, strong) UIColor *tintColor NS_AVAILABLE_IOS(7_0);
-
tintColor
屬性是 iOS 7.0 新增屬性,被使用在UIView
中以改變應(yīng)用程序的外觀(我的理解是,類似于給所有 UI 控件統(tǒng)一設(shè)置主題色效果); - 該屬性是
UIColor
類型,默認(rèn)為系統(tǒng)顏色(藍色); - 它將會運用父視圖層次的顏色來進行著色。可以通過設(shè)置 root view controller 的
tintColor
來改變系統(tǒng)整體的顏色。
特點:繼承、重寫、傳播。
繼承:只要一個 UIView 的 subview 沒有明確指定
tintColor
,那么這個 UIView 的tintColor
就會被它的 subview 所繼承!在一個 App 中,最頂層的 view 就是 window,因此,只要修改 window 的tintColor
,那么所有 view 的tintColor
就都會跟著改變。(這種說法其實并不嚴(yán)謹(jǐn),請耐心繼續(xù)看下去_)。重寫:如果明確指定了某個 view 的
tintColor
, 那么這個 view 就不會繼承其 superview 的tintColor
。而且自此, 這個 view 的 subview 的tintColor
會發(fā)生改變。傳播:一個 view 的
tintColor
的改變會立即向下傳播, 影響其所有的 subview,直至它的一個 subview 明確指定了tintColor
為止。
Tint color of existing iOS controller - 使用tint color為iOS中已經(jīng)存在的控件進行著色
示例代碼:
- (IBAction)changeColorHandle:(id)sender {
// 生成隨機色
CGFloat hue = ( arc4random() % 256 / 256.0 );
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;
UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
// 設(shè)置 tintColor
self.view.tintColor = color;
}
效果:只要修改根視圖控制器的 tintColor
屬性,該頁面下的所有 UI 控件顏色都會隨之改變。
關(guān)于 UIProgressView
參考的文章稱 tintColor
屬性并不會影響 UIProgressView,因為其有兩個描述 tintColor
的屬性:
// 填充進度條的顏色
@property(nonatomic, strong, nullable) UIColor* progressTintColor;
// 未填充進度條的顏色
@property(nonatomic, strong, nullable) UIColor* trackTintColor;
需要進行額外設(shè)置:
- (void)updateProgressViewTint {
self.progressView.progressTintColor = self.view.tintColor;
}
?? 更正
注釋以上代碼后測試,結(jié)果并不是這樣,UIProgressView 的 progressTintColor
值是會跟隨 tintColor
的變化而變化的。
Tint Dimming - 顏色變暗
UIViewTintAdjustmentMode
typedef NS_ENUM(NSInteger, UIViewTintAdjustmentMode) {
UIViewTintAdjustmentModeAutomatic,
UIViewTintAdjustmentModeNormal,
UIViewTintAdjustmentModeDimmed,
} NS_ENUM_AVAILABLE_IOS(7_0);
示例代碼:
- (IBAction)dimTintHandle:(id)sender {
if (self.dimTintSwitch.isOn) {
self.view.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
}else {
self.view.tintAdjustmentMode = UIViewTintAdjustmentModeNormal;
}
}
效果:設(shè)置UIViewTintAdjustmentModeDimmed
屬性以后,所有 UI 控件的顏色會變暗,適合沉浸式體驗的場景。
Using tint color in custom views - 給自定義視圖進行著色
自定義 UIView 子類需要覆蓋以下方法:
/*
The -tintColorDidChange message is sent to appropriate subviews of a view when its tintColor is changed by client code or to subviews in the view hierarchy of a view whose tintColor is implicitly changed when its superview or tintAdjustmentMode changes.
*/
- (void)tintColorDidChange NS_AVAILABLE_IOS(7_0);
創(chuàng)建一個自定義視圖類:
// ***********************************
// HQLCustomView.h
#import <UIKit/UIKit.h>
@interface HQLCustomView : UIView
@end
// ***********************************
// HQLCustomView.m
#import "HQLCustomView.h"
@implementation HQLCustomView {
UIView *_tintColorBlock;
UILabel *_greyLabel;
UILabel *_tintColorLabel;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (!self) {
return nil;
}
self.backgroundColor = [UIColor clearColor];
[self prepareSubviews];
return self;
}
- (void)prepareSubviews {
_tintColorBlock = [[UIView alloc] init];
_tintColorBlock.backgroundColor = self.tintColor;
[self addSubview:_tintColorBlock];
_greyLabel = [UILabel new];
_greyLabel.text = @"Grey Label";
_greyLabel.textColor = [UIColor grayColor];
[_greyLabel sizeToFit];
[self addSubview:_greyLabel];
_tintColorLabel = [UILabel new];
_tintColorLabel.text = @"Tint Color Label";
_tintColorLabel.textColor = self.tintColor;
[_tintColorLabel sizeToFit];
[self addSubview:_tintColorLabel];
}
- (void)layoutSubviews {
_tintColorBlock.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds) / 3, CGRectGetHeight(self.bounds));
CGRect frame = _greyLabel.frame;
frame.origin.x = CGRectGetWidth(self.bounds) / 3 + 10;
frame.origin.y = 0;
_greyLabel.frame = frame;
frame = _tintColorLabel.frame;
frame.origin.x = CGRectGetWidth(self.bounds) / 3 + 10;
frame.origin.y = CGRectGetHeight(self.bounds) / 2;
_tintColorLabel.frame = frame;
}
// 當(dāng)tintColor或者tintAdjustmentMode屬性發(fā)生變化時調(diào)用
- (void)tintColorDidChange {
_tintColorBlock.backgroundColor = self.tintColor;
_tintColorLabel.textColor = self.tintColor;
}
@end
Tinting images with tintColor - 給圖像著色
// 加載圖片
UIImage *gitHubImg = [UIImage imageNamed:@"gitHub"];
// 設(shè)置圖片渲染模式
gitHubImg = [gitHubImg imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
// 添加到視圖
self.tintedImageView.image = gitHubImg;
self.tintedImageView.contentMode = UIViewContentModeScaleAspectFit;
原圖:
實際效果: