由于项目中很少能用到Tint Color所以对其了解一直很模糊.直到最近想对Tint Color有深入些的了解.
关于TintColor,苹果的开发者文档有如下说明:
Tint Color
Views have a tintColor
property that specifies the color of key elements within the view. Each subclass ofUIView
defines its own appearance and behavior for tintColor
. For example, this property determines the color of the outline, divider, and icons on a stepper:
The tintColor
property is a quick and simple way to skin your app with a custom color. Setting a tint color for a view automatically sets that tint color for all of its subviews. However, you can manually override this property for any of those subviews and its descendants. In other words, each view inherits its superview’s tint color if its own tint color is nil
. If the highest level view in the view hierarchy has a nil
value for tint color, it defaults to the system blue color.
tintColor这个属性是view专门用来指定它自己包含的关键元素的颜色的.UIView的子类都可定义自己的tintColor的表现和行为.举个例子,步进控件的tintColor可以决定步进控件的边框线,分隔线和图标颜色等.通过tintColor可以快速且简单直接的改变整个app的皮肤(风格颜色).设置一个view的tintColor可以自动的设置其所有subView的tint color.但是,你可以手动的设置一个view的任何子视图,只要对子视图的tintColor属性重写,也就是说一个view未设置其tint color属性时(nil),它自动继承其父视图的tint color.如果这个view的最顶层的父类的tint color属性值为nil时(默认?),那么这个view的tint color就是系统默认的蓝色.
测试代码及调试结果:
(1)
UIStepper *step = [[ UIStepper alloc]initWithFrame:CGRectMake(100, 100, 100, 60)];
[self.view addSubview:step];
UIStepper *step_ = [[ UIStepper alloc]initWithFrame:CGRectMake(100, 200, 100, 60)];
step_.tintColor = [UIColor orangeColor];
[self.view addSubview:step_];
UIStepper *step_0 = [[ UIStepper alloc]initWithFrame:CGRectMake(100, 300, 100, 60)];
[self.view addSubview:step_0];
(2)
UIStepper *step = [[ UIStepper alloc]initWithFrame:CGRectMake(100, 100, 100, 60)];
[self.view addSubview:step];
self.view.tintColor = [UIColor purpleColor];//新增代码
UIStepper *step_ = [[ UIStepper alloc]initWithFrame:CGRectMake(100, 200, 100, 60)];
step_.tintColor = [UIColor orangeColor];
[self.view addSubview:step_];
UIStepper *step_0 = [[ UIStepper alloc]initWithFrame:CGRectMake(100, 300, 100, 60)];
[self.view addSubview:step_0];
要注意第一个step 的tintColor.
ps:再次感觉到开发者文档的重要性,虽是英文,但有时候一句话让你能够拨云见日.