marty
marty

Reputation: 1137

How to resize UIButton to fit UIImageView?

I have a UIButton over a UIImageView. I want the button to adjust its size to fit the imageview. I tried a few different ways but can't get it. Anyone know how to do this?

Upvotes: 0

Views: 1118

Answers (4)

Piyush
Piyush

Reputation: 244

Try this

    [btn sizeThatFits:CGSizeMake(img.size.width, img.size.height)];

Upvotes: 0

Jyoti Kumari
Jyoti Kumari

Reputation: 45

you can use--

UIImage *img = [UIImage imageNamed:@"7.png"];// image to be use
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];// set button frame accordingly.

Upvotes: 0

Matt Williamson
Matt Williamson

Reputation: 40193

You can add some padding like this:

NSInteger padding = 10;
CGRect newFrame = myImageView.bounds;
newFrame.origin.x += padding / 2.0;
newFrame.origin.y += padding / 2.0;
newFrame.size.width -= padding;
newFrame.size.height -= padding;
myButton.frame = newFrame;

Upvotes: 1

JJgendarme
JJgendarme

Reputation: 1401

Just make a custom button with an image-view in Interface-Builder.

Upvotes: 0

Related Questions