Vannian
Vannian

Reputation: 1530

How to add image as button in objective c?

I want to add image as button, and when it is clicked it should open a new view controller... Does anyone know how to do?

I created a button and added image, but its not nice, because when I click on the image it animates a square arround the image, which I do not want. This what I did.

- (void)setTutorialButtonImage
{
    [self.tutorialButton setImage:[UIImage imageNamed:@"app_logo.png"] forState:UIControlStateNormal];
}

Does any one know how to do... just an image and when I click on it start a new controller

Upvotes: 2

Views: 10936

Answers (3)

CainaSouza
CainaSouza

Reputation: 1437

UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(x, y, width, height)];
[sampleButton setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[sampleButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];

Upvotes: 0

Nils Hott
Nils Hott

Reputation: 1690

You can disable the highlighting effect by using

self.tutorialButton.adjustsImageWhenHighlighted = NO;

Upvotes: 2

nkongara
nkongara

Reputation: 1229

Also try setting the same image for selected and highlighted states too

[button setBackgroundImage:[UIImage imageNamed:@"app_logo.png"] forState: UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:@"app_logo.png"] forState: UIControlStateSelected];

For better look and feel you can apply insets on image using

- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets

Upvotes: 0

Related Questions