jogshardik
jogshardik

Reputation: 1466

UIButton dynamic width in autolayout

Hello guys i have one that is. I have two buttons in controller when i remove or hidden one button i want to other button to increase size according to width. Here i am using the auto layout so please post your answer accordingly

enter image description hereenter image description here

See the above image when i hide the button other button it should be adjust with the size.

Upvotes: 0

Views: 1899

Answers (3)

Hima
Hima

Reputation: 1247

you can add this constraint manually by creating its outlet of "width constraint" like below :

enter image description here

And wherever you want to re-set its width then you can do it manually,

   - (IBAction)buttonClicked:(id)sender 
{
    [UIView animateWithDuration:0.15 animations:^{
        _widthConstraint.constant = 100;
    }];
    [self.view layoutIfNeeded];
    [self.view updateConstraints];
}

Upvotes: 0

André Slotta
André Slotta

Reputation: 14040

  1. set up all the constraints needed to create the layout like in your first screenshot
  2. in IB you select the "time-button" and the "pay by cash" button and add a constraint to align their trailing edges
  3. select this new "trailing-edges-constraint" and set it's priority to high (750)
  4. (repeat the steps 1-3 for the "pay now" button but this time align to the leading edges)

and that's it!

Upvotes: 2

SeanChense
SeanChense

Reputation: 846

FDStackView

Use UIStackView as if it supports iOS 6. It will automatically replace the symbol for UIStackView into FDStackView at runtime before iOS 9.

Or you can using a array of UIView to hold the buttons you want. The first and last element of the array is a 1px width 0.01 alpha UIView. Making the first's left align to the left of screen and the last's right align to the right of screen.

The all you need to do is that put the buttons in the array and make sure every buttons are between the 1px views.

Upvotes: 0

Related Questions