pmerino
pmerino

Reputation: 6130

Programmatically stretch UIButton width depending on condition

I've got two UIButtons into a view, which is 232px wide.

The buttons are aligned such as

|[BUTTON1][BUTTON1]|
|-------232px------|

I have a set of constraints for this alignment, but now I want to be able to change the frame of BUTTON1's to full width, depending on a condition, so it'd have to be programmatically. I tried changing the frame, but since it's automatically updated, it won't work. Basically, what I want to achieve is BUTTON1 covering BUTTON2, Is there an easy way to do this?

Upvotes: 1

Views: 179

Answers (3)

shane
shane

Reputation: 1863

You can set the constant of the button you want covered to 0 and animate the change. Then the covering button will fill in the whole space.

Upvotes: 0

Rohit Kumar
Rohit Kumar

Reputation: 887

Make the outlet of button and width constraint and then

self.buttonWidthConstraint += 20 // adjust as per requirements
[self.myButton updateConstraints]

Upvotes: 1

Rajat
Rajat

Reputation: 11127

As you are using autoLayout in your project, so to change the frame of your button, you have to change the constraint of you UIButton.

First make the IBOutlet of your buttons' width constraint and then when you want to change the frame of the button update that constraint like this

button.widthConstraint.constant = //set the constraint;
[UIView animateWithDuration:0.25f
             animations:^{
                 [self.view setNeedsLayout];
             }
             completion:^(BOOL finished) {
             }];

Upvotes: 1

Related Questions