shajem
shajem

Reputation: 2121

Adding an UIImageView and a Button

I have added an imageview, and i am trying to add an button over it. How to do it.

self.imgview = [UIImageView alloc]initWithImage:[UIImage imageNamed:@"f.png"]];
[self.imgview addSubView:button];

[self.view addSubView:imgView];

What i get is the f.png Image, i dont see the button that i added over it. How could i resolve this ?

I have no idea what is wrong and i have been stuck in this problem for hours. I need your help

Upvotes: 0

Views: 471

Answers (3)

Mateus Mesquita
Mateus Mesquita

Reputation: 36

You need to set the views' frames:

self.imgview = [UIImageView alloc]initWithImage:[UIImage imageNamed:@"f.png"]];
self.button =  [[UIButton alloc] init];

[imgview setFrame:CGRectMake(100,100,100,100)];
[button setFrame:CGRectMake(100,100,100,100)];  

[self.view addSubView:imgView];
[self.view addSubView:button];

Upvotes: 2

Sergey
Sergey

Reputation: 1

Try to use bringSubviewToFront method

Upvotes: 0

bensnider
bensnider

Reputation: 3772

self.imgview = [UIImageView alloc]initWithImage:[UIImage imageNamed:@"f.png"]];
[self.view addSubView:imgView];
[self.view addSubView:button];

Just add them both to the view, adding the button after the image so that it composes on top of the image. Assuming their frame's x,y coordinates also match up.

Upvotes: 0

Related Questions