Unable to resize UIButton

It seems such a simple thing, I can’t understand why this is not happening. I am trying to resize a UIButton from another touchUpInside handler. For playback, I created a new (iOS 6) project in Xcode and dragged two buttons into the xib file, which was created by default. The touchUpInside handler for button1 shown below. If I just turn on the statement that sets origin.x of button2 , I see that button2 moves as expected. However, the statement setting the button size.width buttons does not work. In fact, if I include this operator, the origin also does not change.

I can resize the button in the xib design and the only property I set (except

 - (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 resize the button?

Thanks!

+6
source share
5 answers

Of course you're using AutoLayout, just what happens here is the iOS AutoLayout engine trying to automatically customize your layout. try your code by disabling AutoLayout. hope you know how to disable autoscaling in xcode if you don't tell me.

Happy coding !!!

EDIT:

AutoLayout ships with iOS 6, so your code worked with the old version (iOS 5). By the way, as Nick said, you can disable AutoLayout in the file inspector in xcode.

See attached screenshot.

enter image description here

+7
source

Try:

 CGRect buttonFrame = button.frame; buttonFrame.size = CGSizeMake(150, 70); button.frame = buttonFrame; 
+3
source

Work with borders:

[self.button setBounds:CGRectMake(0, 0, 400, 400)];

Works with auto layout turned on! As always, make sure your output is properly configured so that you can talk to the button.

You can do this in a car layout. Remove the width and height restrictions and add new ones, but if you do not have a width or height limit on your button (maybe wise), then just set the borders and if you do not need to move your button, it should only work as the automatic layout automatically updates restrictions when changing the borders of buttons.

By the way, you should never touch the button frame unless you use it to place the object manually. Borders for calibration, frame for positioning.

0
source

Turning off automatic layout may not be the solution you want.

One possibility is that you set the title assigned by the buttons, and now just changing the font size of the title will not work. You need to set the AttributedTitle again

0
source

If you try to change the length of the UIButton text, you will be limited by the default length, and when it is too long, a line break will be implemented (i.e. Truncate Tail or three dots). Change the default size to:

yourButton.setTitle("yourString", for: UIControlState.normal)

This will work in most cases, but not for all UIControlState s

0
source

All Articles