Question

I will add a label to an Image with addSubview but this does not work.

here the code:

.h
    UIImageView *bgimage;
    IBOutet UILabel *loadingLabel;


.m
    loadingLabel.text =@"......";
    bgimage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
    bgimage.image = [UIImage imageNamed:@"Wait.png"];
    [self.view addSubview:bgimage];
    [bgimage addSubview:loadingLabel];

Screenshot:

Screenshot

"Tisch wird reserviert..." is the loadinglabel and is label shoud be in the UiImage

Screenshot 2

This is the "Layout" with the code from 2nd answer

Was it helpful?

Solution

Change your code as...

.h

IBOutet UIImageView *bgimage;
UILabel *loadingLabel;

.m

bgimage.image = [UIImage imageNamed:@"Wait.png"];
loadingLabel = [[UILabel alloc] init];
loadingLabel.frame = CGRectMake(0, 0, 100, 50);
loadingLabel.text = @"testing";
[bgimage addSubview:loadingLabel];

It will work.

OTHER TIPS

You cant add a subview to a uiimageview, since drawRect: will never be called. source: "The "UIImageView class is optimized to draw its images to the display. UIImageView will not call the drawRect: method of a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class." from the apple docs:https://www.google.de/#bav=on.2,or.r_qf.&fp=90e2434f04e1ee9b&q=uiimageview+class+reference&safe=off

Solution: As suggested, use a uiview to contain both the label and the imageview, and then bring the label to the top using -bringSubviewtoFront: of UIView. You can also use a UIViews backgroundimage to show your image and then have a label as a subclass in that view. Depends on the Situation you are in, i guess.

EDIT: YOU SHOULD READ THIS: I misread the question, and it appears that you can add subviews to UIImageView, just like Zev pointed out in his comment. Right now im guessing that -bringSubviewToFront did the trick for you, and that the rest of my answer, while not really harmful, was unnecessary. Im sorry.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top