tintColor

作者: 冷武橘 | 来源:发表于2021-04-13 11:15 被阅读0次

1、tintColor

@property(null_resettable, nonatomic,strong) UIColor *tintColor;
定义了一个非默认的着色颜色值,其值的设置会影响到以视图为根视图的整个视图层次结构。

    UIView *view =[[UIView alloc]init];
    [self.view addSubview:view];
    view.frame = CGRectMake(0, 0, 300, 300);
    view.backgroundColor = [UIColor redColor];
    UIView *view1 =[[UIView alloc]init];
    [view addSubview:view1];
    view1.frame = CGRectMake(60, 60, 220, 220);
    view1.tintColor = [UIColor greenColor];
    view1.backgroundColor = [UIColor greenColor];
    
    UILabel *lable = [[UILabel alloc]init];
    [self.view addSubview:lable];
    lable.backgroundColor = [UIColor purpleColor];
    lable.frame = CGRectMake(20, 320, 200, 40);
    lable.textColor = view.tintColor;
    lable.text = @"当前view得tintcolor";
    UILabel *lable1 = [[UILabel alloc]init];
    [self.view addSubview:lable1];
    lable1.backgroundColor = [UIColor grayColor];
    lable1.frame = CGRectMake(20, 370, 200, 40);
    lable1.textColor = view1.tintColor;
    lable1.text = @"当前view1的tintcolor”;

一个view的tintColor默认是nil,这意味着视图将使用父视图的tint color值。当我们指定了一个视图的tintColor后,这个色值会自动传播到视图层次结构(以当前视图为根视图)中所有的子视图上。如果系统在视图层次结构中没有找到一个非默认的tintColor值,则会使用系统定义的颜色值(蓝色,RGB值为[0,0.478431,1],我们可以通过上面的view、lable看到这个颜色)。

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:btn];
    [btn setTitle:@"测试" forState:UIControlStateNormal];
    btn.frame = CGRectMake(100, 100, 100, 100);

正如上面创建一个系统默认按钮btn,没有设置setTitleColor,但是title color却是蓝色的,那是因为btn的title color自动取了自己tintColor的值。我们可以通过添加这句 [btn setTintColor:[UIColor redColor]];代码,运行后title color变为了红色,得以验证。

我们可以子类化重写tintColorDidChange来实现自定义view类似上面的效果。

@implementation MyLable

- (void)tintColorDidChange{
    [super tintColorDidChange];
    self.textColor = self.tintColor;
}
@end


    MyLable *lable =[[MyLable alloc]init];
    [self.view addSubview:lable];
    lable.frame = CGRectMake(100, 100, 100, 40);
    lable.tintColor = [UIColor redColor];
    lable.text = @"测试";
   

这个方法会在视图的tintColor或tintAdjustmentMode属性改变时自动调用。另外,如果当前视图的父视图的tintColor或tintAdjustmentMode属性改变时,也会调用这个方法。我们可以在这个方法中根据需要去刷新我们的视图。

2、tintAdjustmentMode

@property(nonatomic) UIViewTintAdjustmentMode tintAdjustmentMode

typedef NS_ENUM(NSInteger, UIViewTintAdjustmentMode) {
    UIViewTintAdjustmentModeAutomatic, 视图的着色调整模式与父视图一致
    UIViewTintAdjustmentModeNormal, 视图的tintColor属性返回完全未修改的视图着色颜色
    UIViewTintAdjustmentModeDimmed,  视图的tintColor属性返回一个去饱和度的、变暗的视图着色颜色
} ;

tintAdjustmentMode的作用通俗来讲就是决定tintcolor的最终效果,比如tintcolor是red,当你将tintAdjustmentMode设置成UIViewTintAdjustmentModeDimme,red将会被变暗,tintcolor的颜色效果就不是红色;当你将tintAdjustmentMode设置成 UIViewTintAdjustmentModeNormal,tintcolor的颜色效果就是原本的颜色,没有任何改变。那么我们怎么去理解UIViewTintAdjustmentModeAutomatic呢?可以通过以下例子

    UIView *view =[[UIView alloc]init];
    view.tintColor = [UIColor blueColor];
    [self.view addSubview:view];
    view.frame = CGRectMake(0, 0, 300, 300);
    view.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
    view.backgroundColor = view.tintColor;

    
    UIView *view1 =[[UIView alloc]init];
    [view addSubview:view1];
    view1.frame = CGRectMake(60, 60, 220, 220);
    view1.tintColor = [UIColor greenColor];
    view1.tintAdjustmentMode =  UIViewTintAdjustmentModeAutomatic;
    view1.backgroundColor = view1.tintColor;

正如代码所示:如果你希望子视图view1随着父视图view的颜色效果就可以设置view1.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic;

3、image的渲染

@property(nonatomic, readonly) UIImageRenderingMode renderingMode

typedef NS_ENUM(NSInteger, UIImageRenderingMode) {
    UIImageRenderingModeAutomatic,   根据图片的使用环境和所处的绘图上下文自动调整渲染模式。
    UIImageRenderingModeAlwaysOriginal,    始终绘制图片原始状态,不使用Tint Color。  
    UIImageRenderingModeAlwaysTemplate,  始终根据Tint Color绘制图片,忽略图片的颜色信息。
}   

利用 UIImageRenderingModeAlwaysTemplate和tintcolor我们可以使用一张镂空的图片去渲染不同颜色的图片,比如不同颜色导航条的设置

    UIButton *backButton =[[UIButton alloc]init];
    backButton.frame = CGRectMake(0, 0, 20, 20);
   backButton.backgroundColor = [UIColor redColor];
   UIImage *image = [[UIImage   imageNamed:@"1.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];  
    backButton.tintColor = [UIColor lightGrayColor];
   [backButton setImage:image forState:UIControlStateNormal];
   UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];    self.navigationItem.rightBarButtonItem = rightButtonItem;

4、barTintColor

barTintColor影响着nabar和tabar的背景颜色
[self.navigationController.navigationBar setBarTintColor: [UIColor orangeColor]];

5、tabar

tabar默认情况下,当tabBarItem被选中时文字、图标会被渲染成蓝色,因为系统默认tintcolor是蓝色。当你想要当tabBarItem被选中时文字、图片会被渲染成红色,你只需要设置 tabBar.tintColor = [UIColor redColor];

相关文章

网友评论

      本文标题:tintColor

      本文链接:https://www.haomeiwen.com/subject/aqkxlltx.html