在标签下设置UIImageView,而不是给出坐标



如果你想创建一个ImageView,那么你需要这样创建它:

    UIImageView *imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 20)] autorelease];

x和y坐标是10,但如果你有一个标签或其他东西(一个textview,你不知道有多长文本将是),那是在完全相同的地方。你怎么能让图像总是显示在标签下,而不是给出坐标你能怎么做?

有两种方法。

1。添加你的图像视图到你的视图之后,添加标签或任何东西到你的视图,然后它重叠的图像视图,但给backGroundColor清除颜色。

2。将标签添加到imageview,并将imageview添加到Self.view。

IMP:清除放置在UIImageView上的对象的backGroundColor

UIImageView *imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(_yourLabel.frame.origin.x, _yourLabel.frame.origin.y, 100, 20)] autorelease];

:

[yourView addSubview:imgView];
[yourView addSubview:_yourLabel];

设置高度和宽度也等于UILabel

UIImageView *imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(yourLabel.frame.origin.x, yourLabel.frame.origin.y, yourLabel.frame.size.width,yourLabel.frame.size.height)] autorelease];
[self addSubview:imgView];
[self addSubview:yourLabel];

最新更新