uikit——UIView——tintColor tintAdjustmentMode

tintColor&tintAdjustmentMode

/*
 -tintColor always returns a color. The color returned is the first non-default value in the receiver's superview chain (starting with itself).
 If no non-default value is found, a system-defined color is returned.
 If this view's -tintAdjustmentMode returns Dimmed, then the color that is returned for -tintColor will automatically be dimmed.
 If your view subclass uses tintColor in its rendering, override -tintColorDidChange in order to refresh the rendering if the color changes.
 */
@property(null_resettable, nonatomic, strong) UIColor *tintColor NS_AVAILABLE_IOS(7_0);

/*
 -tintAdjustmentMode always returns either UIViewTintAdjustmentModeNormal or UIViewTintAdjustmentModeDimmed. The value returned is the first non-default value in the receiver's superview chain (starting with itself).
 If no non-default value is found, UIViewTintAdjustmentModeNormal is returned.
 When tintAdjustmentMode has a value of UIViewTintAdjustmentModeDimmed for a view, the color it returns from tintColor will be modified to give a dimmed appearance.
 When the tintAdjustmentMode of a view changes (either the view's value changing or by one of its superview's values changing), -tintColorDidChange will be called to allow the view to refresh its rendering.
 */
@property(nonatomic) UIViewTintAdjustmentMode tintAdjustmentMode NS_AVAILABLE_IOS(7_0);

/*
 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);
typedef NS_ENUM(NSInteger, UIViewTintAdjustmentMode) {
    UIViewTintAdjustmentModeAutomatic,
    
    UIViewTintAdjustmentModeNormal,
    UIViewTintAdjustmentModeDimmed,
} NS_ENUM_AVAILABLE_IOS(7_0);
解释:
  • tintColor默认为系统定义颜色,tintColor值改变,更新view hierarchy的tintColor值,并调用tintColorDidChange
  • tintAdjustmentMode默认为UIViewTintAdjustmentModeNormal,tintAdjustmentMode值改变,更新view hierarchy的tintAdjustmentMode值,并调用tintColorDidChange
  • tintAdjustmentMode值为UIViewTintAdjustmentModeDimmed时,会对tintColor进行灰色处理
  • tintColor不支持pattern image,如果tintColor为pattern image,throw exception

应用

@interface FBView : UIView

@end

@implementation FBView

- (void)tintColorDidChange
{
    NSLog(@"tintColorDidChange: tag = %ld, tintColor = %@, tintAdjustmentMode = %lu", self.tag, self.tintColor, self.tintAdjustmentMode);
}

@end
- (void)tint
{
    FBView *view = [[FBView alloc] initWithFrame:CGRectZero];
    FBView *view1 = [[FBView alloc] initWithFrame:CGRectZero];
    FBView *view2 = [[FBView alloc] initWithFrame:CGRectZero];
    FBView *view1_1 = [[FBView alloc] initWithFrame:CGRectZero];
    FBView *view1_2 = [[FBView alloc] initWithFrame:CGRectZero];
    
    view.tag = 5;
    view1.tag = 1;
    view2.tag = 2;
    view1_1.tag = 11;
    view1_2.tag = 12;
    
    [self.view addSubview:view];
    [view addSubview:view1];
    [view addSubview:view2];
    [view1 addSubview:view1_1];
    [view1 addSubview:view1_2];
    
    NSLog(@"default: tag = %ld, tintColor = %@, tintAdjustmentMode = %lu", view.tag, view.tintColor, view.tintAdjustmentMode);
    NSLog(@"change tintColor");
    view.tintColor = [UIColor blueColor];
    NSLog(@"change tintAdjustmentMode");
    view.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
}
output:
default: tag = 5, tintColor = UIDeviceRGBColorSpace 0 0.478431 1 1, tintAdjustmentMode = 1
change tintColor
tintColorDidChange: tag = 5, tintColor = UIDeviceRGBColorSpace 0 0 1 1, tintAdjustmentMode = 1
tintColorDidChange: tag = 1, tintColor = UIDeviceRGBColorSpace 0 0 1 1, tintAdjustmentMode = 1
tintColorDidChange: tag = 11, tintColor = UIDeviceRGBColorSpace 0 0 1 1, tintAdjustmentMode = 1
tintColorDidChange: tag = 12, tintColor = UIDeviceRGBColorSpace 0 0 1 1, tintAdjustmentMode = 1
tintColorDidChange: tag = 2, tintColor = UIDeviceRGBColorSpace 0 0 1 1, tintAdjustmentMode = 1
change tintAdjustmentMode
tintColorDidChange: tag = 5, tintColor = UIDeviceWhiteColorSpace 0.11 0.8, tintAdjustmentMode = 2
tintColorDidChange: tag = 1, tintColor = UIDeviceWhiteColorSpace 0.11 0.8, tintAdjustmentMode = 2
tintColorDidChange: tag = 11, tintColor = UIDeviceWhiteColorSpace 0.11 0.8, tintAdjustmentMode = 2
tintColorDidChange: tag = 12, tintColor = UIDeviceWhiteColorSpace 0.11 0.8, tintAdjustmentMode = 2
tintColorDidChange: tag = 2, tintColor = UIDeviceWhiteColorSpace 0.11 0.8, tintAdjustmentMode = 2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值