发送UIImageView(标签背景)以显示UILabel

时间:2013-05-13 18:29:58

标签: uiimageview uilabel

我正在尝试将图像作为背景添加到UILabel,但是我的UILabel标题无法看到,即使我尝试将背景图像发送到背面。我的代码如下,任何关于如何帮助这个的建议都会很棒,谢谢!

UIImageView *labelBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];
[myLabel addSubview:labelBackground];
[myLabel sendSubviewToBack:labelBackground];
myLabel.backgroundColor = [UIColor clearColor];
[myLabel setText:title];

2 个答案:

答案 0 :(得分:1)

尝试添加UILabel作为UIImageView的子视图,而不是相反。

这将导致UIImageView成为标签的“容器”,因此标签位于UIImageView的顶部。

答案 1 :(得分:1)

当您将视图(图像视图)作为子视图添加到另一个视图(您的标签)时,子视图将始终位于其超级视图的前面。他们要么是兄弟姐妹,要么是

[myContainer addSubview:labelBackground];
[myContainer addSubview:myLabel];

或者更好的是,标签应该是图像视图的子视图:

[labelBackground addSubview:myLabel];
[myContainer addSubView:labelBackground];

另一种解决方案可能是将您的图片用作标签的背景颜色:

[myLabel setBackgroundColor:[UIColor colorWithPatternImage:myUIImage]];

但请注意,图像将重复而不是居中或拉伸。