当某些视图隐藏时,如何调整UIView宽度?

时间:2014-02-04 08:44:29

标签: ios iphone objective-c uiview

enter image description here

上面的图片是从storyboard捕获的。 它的构造如下: UIView(包括UILabelUIProgressView)< - 2px spacing - > UIActivityIndicatorView< --- 6px间距---> UIButton

我希望UIView在隐藏UIActivityIndicatorView时将其宽度增加到按钮,如下所示:    UIView(包括UILabelUIProgressView)< --- 6 space ---> UIButton

我该怎么做?让我知道。请。

3 个答案:

答案 0 :(得分:1)

https://github.com/bilobatum/ActivityIndicatorDemo

enter image description here

使按钮的固有内容大小沿所需的水平轴(即优先级1000)。这可以防止布局沿水平轴模糊。

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

答案 1 :(得分:0)

如果您使用自动布局,请考虑在禁用UIActivityIndi​​catorView时将其设置为0。这不仅仅是你所需要的,但它已经接近了。

答案 2 :(得分:0)

我们假设您的UILabel被称为_labelUIProgressView被称为_progressViewUIActivityIndicator被称为_activityIndicator

隐藏_activityIndicator时,请调用此代码块:

[_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)];

再次显示_activityIndicator时:

[_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)];

请记住,如果您想手动设置帧,则应关闭Autolayout!这很简单:只需取消选中File Inspector中的“Use Autolayout”功能:

enter image description here