UIView设置色彩

时间:2014-11-06 10:09:50

标签: ios iphone

我正在创建一个像标签栏一样的视图。为此我设置了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];  

但我无法看到这个观点。但是,当我取消注释背景颜色代码行时,它会显示但没有色调效果 我怎样才能做到这一点。

2 个答案:

答案 0 :(得分:0)

首先,为什么在刚将子视图添加到视图时使用[self.view bringSubviewToFront:bottomTab];

当您向视图添加子视图时,它具有最高的z-Index,因此无论如何它都会在前面。

其次,您没有看到视图的原因,是因为默认情况下它具有透明背景。 TintColor不会更改背景颜色,只会更改您放置在视图中的项目的色调。

您只需在视图中添加一个按钮即可对此进行测试:

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];

答案 1 :(得分:0)

我为此创建了宏:

#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);