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

Ios 当某些视图隐藏时,如何调整UIView宽度?,ios,iphone,objective-c,uiview,Ios,Iphone,Objective C,Uiview,上面的图像是从情节提要中捕获的。 其构造如下所示: UIView(包括UILabel和UIProgressView)UIActivityIndicatorViewUIButton 当UIActivityIndicatorView隐藏时,我希望UIView将其宽度增加到按钮,如下所示: ui视图(包括ui标签和ui进度视图)ui按钮 我该怎么做??让我知道。请。 如果您使用自动布局,请考虑禁用UIActuviyDeaCaltoVIEW的宽度为0。这不完全是你需要的,但很接近 假设您的UILabel

上面的图像是从
情节提要
中捕获的。 其构造如下所示:
UIView
(包括
UILabel
UIProgressView
UIActivityIndicatorView
UIButton

UIActivityIndicatorView
隐藏时,我希望
UIView
将其宽度增加到按钮,如下所示:
ui视图
(包括
ui标签
ui进度视图
ui按钮


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

如果您使用自动布局,请考虑禁用UIActuviyDeaCaltoVIEW的宽度为0。这不完全是你需要的,但很接近

假设您的
UILabel
被称为
\u label
UIProgressView
被称为
\u progressView
UIActivityIndicator
被称为
\u activityIndicator

隐藏
\u 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)];
当活动指示器再次显示时:

[_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
!非常简单:只需取消选中文件检查器中的“使用自动布局”功能:

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

@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


有两种方法可以做到这一点:在UIActivityIndicatorView隐藏/显示时设置该视图的框架;或者使用自动布局;在UIActivityIndicatorView隐藏/显示时更改宽度约束内容。你可以在谷歌上找到很多组织良好的双向教程谢谢。我发现将约束更改为0是可行的,但它不起作用。你能链接一下改变宽度限制吗?谢谢你的回复。但是我在其他组件上使用自动布局。所以我想使用自动布局。是否有其他具有自动布局的解决方案?无法使用以下代码将其应用于0。CGRect rect=self.activityView.frame;rect.size.width=0;self.activityView.frame=rect@nao0811ta使用灰色视图的子视图更新了答案。GitHub项目也更新了。非常感谢。我很抱歉经常问你这个问题。我是初学者。图特