How to make clickable UIButton over UIImageView?

12,863

Solution 1

i tried this and it works for me.......

UIImageView * imageView = [[UIImageView alloc]init];
[imageView setImage:[UIImage imageNamed:@"image.jpeg"]];
[imageView setFrame:CGRectMake(10,10,300,300)];
[imageView setContentMode:UIViewContentModeScaleToFill];
[imageView setUserInteractionEnabled:TRUE];

UIButton * ButtonOnImageView = [[UIButton alloc]init];
[ButtonOnImageView setFrame:CGRectMake(135,120,60,30)];
[ButtonOnImageView setImage:[UIImage imageNamed:@"image.jpeg"] forState:UIControlStateHighlighted];
 [ButtonOnImageView setImage:[UIImage imageNamed:@"image2.jpeg"] forState:UIControlStateNormal];
[ButtonOnImageView addTarget:self action:@selector(OnClickOfTheButton) forControlEvents:UIControlEventTouchUpInside];

 [imageView addSubview:ButtonOnImageView];
 [self.view addSubview:imageView];

Solution 2

You can use just one button and say:

[UIButton setImage:[imageNamed:@"image.png"]];

Solution 3

  1. you have to add define button as custom.It will work for you.

    UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];

Solution 4

you need to add the button as a subclass of a

scrollview

not

imageview

.. it wont be clickable if the button is a subclass of a uiimageview .. assign a speacial tag for each button to recognize the clicked button

hope this helps

Solution 5

try setting the userinteraction of the button enabled.

[btn setEnabled:YES];

Coz the UIImageView will set the userinteraction of the button disabled by default.

Else you could try this.Just change the last two lines.

Replace:

[imageView addSubview:btn];
[self addSubview:imageView]; 

with this:

[self addSubview:imageView];
[self addSubview:btn];

Make sure you add the imageview first and the button second as i have mentioned. else the button would b placed behind the imageview and would seems hidden.

Share:
12,863
Wahib Ul Haq
Author by

Wahib Ul Haq

http://wahibhaq.com/about-me/bio/

Updated on June 14, 2022

Comments

  • Wahib Ul Haq
    Wahib Ul Haq almost 2 years

    I want to create a UIButton to display over an UIImageView. My code snippet is as shown below:

    imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image
    
    imageView.userInteractionEnabled = YES; //to enable touch interaction with image
    
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setTitle:@"ClickMe" forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    
    
     btn.frame = CGRectMake(340, 550, 70, 50);
    
    [btn addTarget:self action:@selector(onClickHotSpotButton)   forControlEvents:UIControlEventTouchUpInside]; //it wasnt working
    
    
    [imageView addSubview:btn];
    [self addSubview:imageView]; 
    

    My application displays button exactly on part of image where i want to but it doesn't responds to click image. Instead it does detect single tap as i log output on its detection. But i want this button to get clicked on selection and to perform some function.

    Thanks in advance.

    wahib

  • Wahib Ul Haq
    Wahib Ul Haq over 12 years
    Thanks for your response. Whether its a imagebutton or not doesnt matter to me. All i want is to make it clickable. I have two choices rite now. Whether i make my button a subview of ImageView or ScrollView which is parent of ImageView. If i make button subview of Scrollview like [self addSubView:btn]; it displays at right position and is clickable but problem is that it is created on all images in the queue because i am making it a subview of parent view i.e scrollview. Else if i make it subview of child view i.e ImageView which is unique for every image, it is not clickable :/
  • nurnachman
    nurnachman about 12 years
    your solution seems the most elegant
  • tcurdt
    tcurdt almost 11 years
    If you set the images on the button why would you still use the UIImageView?