views:

518

answers:

3

I'm trying to resize a UIButton in my iPhone app. I have a UIButton synthesized and when I call the following code, it moves on the screen, but the width & height of the button never change.

button.frame.size = CGRectMake(104, 68, 158, 70);

For example, when I change the height (70) to 40, the height of the button does not change. If I change the x or y, however, it will move on the screen.

Any ideas?

A: 

I believe you can just adjust the UIButton's frame to adjust the dem.

Rev316
+2  A: 

The code above should not compile; you're trying to assign a CGRect to a CGSize. The problem comes in the misleading nature of properties. What you're doing above is akin to this:

[button frame].size = CGSizeMake(150, 70);

Obviously, this won't actually change the size. What you need to do is grab the frame (or bounds), modify it, and then set it. Try this:

CGRect      buttonFrame = button.frame;
buttonFrame.size = CGSizeMake(150, 70);
button.frame = buttonFrame;
Ben Gottlieb
I just put your code example above into my app (replaced my code) and it essentially does the same thing as mine did. It moves the button up (vertically) on the screen, but does not change it's size.I should add that when I dragged this button onto my view in IB, I set it to "Custom" button. I also have a background image on the button and I have the view mode set to "Bottom."I've done stuff like this before in other languages, and I know this is something simple, but the behavior of moving on the screen verses changing size is very odd.
Adam
Your button is probably changing sizes, but your bitmap is not. You might want to use a stretchable image for the button's background, instead of a fixed image.
Ben Gottlieb
A: 

You MUST call [view setNeedsLayout] on the view where the button is placed.

buttonIBOutlet.frame = CGRectMake(104, 68, 158, 70);
[delegate.window setNeedsLayout];