[英]UIView set tint color
我正在创建一个像标签栏一样的视图。 为此,我设置tintColor:
UIView *bottomTab = [[UIView alloc]initWithFrame:CGRectMake(0,SCREEN_HEIGHT-yVal-bottomBarButtonHeight,SCREEN_WIDTH,75)];
//bottomTab.backgroundColor = [UIColor blackColor];
bottomTab.tintColor = [UIColor blackColor];
[self.view addSubview:bottomTab];
[self.view bringSubviewToFront:bottomTab];
但是我看不到风景。 但是,当我取消注释背景色代码行时,它会出现但没有色彩效果。
我该如何实现。
首先,为什么要使用[self.view bringSubviewToFront:bottomTab];
当您刚刚将子视图添加到视图时?
将子视图添加到视图时,它具有最高的z-Index,因此无论如何它都将位于最前面。
其次,之所以看不到视图,是因为默认情况下它具有透明背景。 TintColor不会更改背景色,而只会更改您在视图中放置的项目的淡色。
您可以简单地通过在视图中添加一个Button来进行测试:
UIView *bottomTab = [[UIView alloc]initWithFrame:CGRectMake(0,SCREEN_HEIGHT-yVal-bottomBarButtonHeight,SCREEN_WIDTH,75)];
bottomTab.tintColor = [UIColor blackColor];
[self.view addSubview:bottomTab];
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
testButton.frame = bottomTab.bounds;
[bottomTab addSubview:testButton];
我为此创建了宏:
#define removeTint(view) \
if ([((NSNumber *)[view.layer valueForKey:@"__hasTint"]) boolValue]) {\
for (CALayer *layer in [view.layer sublayers]) {\
if ([((NSNumber *)[layer valueForKey:@"__isTintLayer"]) boolValue]) {\
[layer removeFromSuperlayer];\
break;\
}\
}\
}
#define setTint(view, tintColor) \
{\
if ([((NSNumber *)[view.layer valueForKey:@"__hasTint"]) boolValue]) {\
removeTint(view);\
}\
[view.layer setValue:@(YES) forKey:@"__hasTint"];\
CALayer *tintLayer = [CALayer new];\
tintLayer.frame = view.bounds;\
tintLayer.backgroundColor = [tintColor CGColor];\
[tintLayer setValue:@(YES) forKey:@"__isTintLayer"];\
[view.layer addSublayer:tintLayer];\
}
要使用,只需调用:
setTint(yourView, yourUIColor);
//Note: include opacity of tint in your UIColor using the alpha channel (RGBA), e.g. [UIColor colorWithRed:0.5f green:0.0 blue:0.0 alpha:0.25f];
删除色彩时,只需调用:
removeTint(yourView);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.