cdub
cdub

Reputation: 25701

How to make a UIImageView a button?

I have a UIImageView like so:

UIImage *image = [UIImage imageNamed:@"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

imageView.frame = CGRectMake(0, 0, 30, 30);
imageView.layer.cornerRadius = 5.0;
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
imageView.layer.borderWidth = 0.1;

[self.view addSubview:imageView];

Couple of issues/questions:

  1. How do I make this a clickable button?
  2. I want to link to Settings (in my app) but do I have to create that image wheel like the settings icon that so many apps use or is it standard icon in iOS 7? This is the icon I'm talking about: iOS Settings Icon

  3. Why doesn't my border show up?

Upvotes: 0

Views: 2082

Answers (4)

M David
M David

Reputation: 260

1:if you have image view and want to clickable

UIImage *image = [UIImage imageNamed:@"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

2: Add gesture to image view like this, but don’t forget to setUserInteractionEnabled:YES

UITapGestureRecognizer *TapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(markerMove:)];

    [TapGestureRecognizer setNumberOfTapsRequired:1];
    [TapGestureRecognizer setNumberOfTouchesRequired:1];
    [imageView setUserInteractionEnabled:YES];
    [imageView addGestureRecognizer: TapGestureRecognizer];

Upvotes: 0

Bernhard Grabowski
Bernhard Grabowski

Reputation: 469

Try:

UIImage *image = [UIImage imageNamed:@"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

imageView.frame = CGRectMake(0, 0, 30, 30);
imageView.layer.cornerRadius = 5.0;
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
imageView.layer.borderWidth = 0.1;

/* Here's the button Start */
UIButton *theButton = [[UIButton alloc]initWithFrame:CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, imageView.frame.size.width, imageView.frame.size.height)];
theButton.backgroundColor = [UIColor clearColor];
theButton.showsTouchWhenHighlighted = YES;
[theButton addTarget:self action:@selector(yourAction) forControlEvents:UIControlEventTouchUpInside];

[imageView addSubview:theButton];
/* Here's the button End */

[self.view addSubview:imageView];

Your border:

0.1 means is 1/10th of a point - try 1.0 for a nice fine line

Also set:

imageView.clipsToBounds = YES;

Upvotes: 0

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

here 2 choice u have to use your method

.h file

 #import <QuartzCore/QuartzCore.h>  //for radius and corner



1. UIButton


   UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
 [btn1 setTitle:@"Cool title" forState:UIControlStateNormal];
[btn1 setFrame:CGRectMake(7, 7, 150, 160)];
[btn1 setImage:[UIImage imageNamed:@"test.png"] forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(selectFav) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];


-(void) selectFav

{  //what ever you do like


 }

2. UITap Gesture for UIImage View


 UIImage *image = [UIImage imageNamed:@"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

  imageView.frame = CGRectMake(0, 0, 30, 30);
  imageView.layer.cornerRadius = 5.0;
  imageView.layer.masksToBounds = YES;
 imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
imageView.layer.borderWidth = 0.1;


   UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapGesture:)];

 tapGesture1.numberOfTapsRequired = 1;

[tapGesture1 setDelegate:self];

 [imgView addGestureRecognizer:tapGesture1];

[self.view addSubview:imageView];

- (void) tapGesture: (id)sender
{
 //handle Tap...
}

Upvotes: 0

Shai
Shai

Reputation: 25619

1) If you want a button, you should use a UIButton and use [UIButton setImage: forState:]

2) Alternatively, you can add a UITapGestureRecognizer to your current UIImageView

Upvotes: 2

Related Questions