nao0811ta
nao0811ta

Reputation: 193

How can I resize UIView width when some view hidden?

enter image description here

The above Image is captured from storyboard. It is constructed like as follows: UIView (include UILabel and UIProgressView) <--2px spacing --> UIActivityIndicatorView <---6px spacing ---> UIButton

I want the UIView to increase its width to button when UIActivityIndicatorView hidden as follows: UIView( include UILabel and UIProgressView) <---6 space---> UIButton

How can I do that?? Let me Know. Please.

Upvotes: 1

Views: 2196

Answers (3)

bilobatum
bilobatum

Reputation: 8918

https://github.com/bilobatum/ActivityIndicatorDemo

enter image description here

Make the button's intrinsic content size along the horizontal axis required (i.e., priority 1000). This prevents the layout from being ambiguous along the horizontal axis.

enter image description here

@interface ViewController ()
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *activityIndicatorWidthConstraint;
@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
// spacerConstraint is the horizontal spacer constraint between the activity indicator and the gray view
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *spacerConstraint;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.activityIndicator startAnimating];
}

- (IBAction)stopButtonTapped:(UIButton *)sender
{
    sender.enabled = NO;

    self.activityIndicatorWidthConstraint.constant = 0;
    self.spacerConstraint.constant = 0;

    [UIView animateWithDuration:1.0 animations:^{
        self.activityIndicator.alpha = 0.0;
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
       [self.activityIndicator stopAnimating];
        //[NSLayoutConstraint reportAmbiguity:nil];
    }];
}

@end

Upvotes: 1

etolstoy
etolstoy

Reputation: 1798

Let's assume that your UILabel is called _label, UIProgressView is called _progressView and UIActivityIndicator is called _activityIndicator.

When you hide your _activityIndicator, call this block of code:

[_label setFrame:CGRectMake(_label.frame.origin.x, _label.frame.origin.y, _label.frame.size.width + _activityIndicator.frame.size.width + 2, _label.frame.size.height)];
[_progressView setFrame:CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y, _progressView.frame.size.width + _activityIndicator.frame.size.width + 2, _progressView.frame.size.height)];

When _activityIndicator shows again:

[_label setFrame:CGRectMake(_label.frame.origin.x, _label.frame.origin.y, _label.frame.size.width - _activityIndicator.frame.size.width - 2, _label.frame.size.height)];
[_progressView setFrame:CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y, _progressView.frame.size.width - _activityIndicator.frame.size.width - 2, _progressView.frame.size.height)];

Remember, that Autolayout should be turned off, if you want to manually set the frames! It is very simple: just uncheck the "Use Autolayout" feature in File Inspector:

enter image description here

Upvotes: 0

Daniel
Daniel

Reputation: 9040

If you are using Auto-layout, consider setting the width of the UIActivityIndicatorView to 0 when you disable it. This won't be exactly what you need, but it's close.

Upvotes: 0

Related Questions