关于Tint Color

这篇博客探讨了iOS中Tint Color的概念及其在视图中的应用。苹果的开发者文档指出,Tint Color用于定义视图关键元素的颜色,每个UIView子类可以有自己的表现和行为。设置一个视图的Tint Color会影响其所有子视图,除非子视图单独设置了Tint Color。未设置Tint Color的视图将继承父视图的Tint Color,若最高层级视图的Tint Color为nil,则默认为系统蓝色。博客通过代码示例展示了Tint Color如何影响UIStepper的外观,并强调了阅读开发者文档的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于项目中很少能用到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:再次感觉到开发者文档的重要性,虽是英文,但有时候一句话让你能够拨云见日.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老槽哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值