Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't change the size of a UIButton

Tags:

ios

ios6

uibutton

This seems such a simple thing, I can't understand why it's going wrong. I'm trying to change the size of a UIButton from the touchUpInside handler of another. To repro, I've created a new (iOS 6) project in Xcode, and dragged two buttons to the xib file that's created by default. The touchUpInside handler for button1 is shown below. If I just include the statement that sets the origin.x of button2, I see button2 move as expected. However, the statement that sets the size.width of the button doesn't work. In fact, if I include that statement the origin doesn't change either.

I can resize the button fine in the xib designer and the only property I've set (other than

- (IBAction)buttonPressed:(id)sender {
    CGRect f = self.button2.frame;
    f.origin.x += 10;
    // this doesn't work, including it prevents even the origin change from working
    f.size.width += 10; 
    self.button2.frame = f;
}

How can I successfully change the size of the button?

Thanks!

like image 827
mcobrien Avatar asked Jan 15 '13 07:01

mcobrien


2 Answers

For sure you are using AutoLayout, simply what happen here is iOS AutoLayout mechanism tries to set your layout automatically. try your code by turning off AutoLayout. hope you know how to turn off the auto layout in xcode, if not let me know.

Happy coding!!!

EDIT:

AutoLayout comes with iOS 6, thats why your code worked with older version(iOS 5). btw as Nick said you can turn off AutoLayout in the file inspector in the xcode.

See attached screenshot.

enter image description here

like image 55
Prasad De Zoysa Avatar answered Nov 15 '22 06:11

Prasad De Zoysa


Try :

CGRect buttonFrame = button.frame;
buttonFrame.size = CGSizeMake(150, 70);
button.frame = buttonFrame;
like image 29
Loris1634 Avatar answered Nov 15 '22 06:11

Loris1634