上面的图片是从storyboard
捕获的。
它的构造如下:
UIView
(包括UILabel
和UIProgressView
)< - 2px spacing - > UIActivityIndicatorView
< --- 6px间距---> UIButton
我希望UIView
在隐藏UIActivityIndicatorView
时将其宽度增加到按钮,如下所示:
UIView
(包括UILabel
和UIProgressView
)< --- 6 space ---> UIButton
我该怎么做?让我知道。请。
答案 0 :(得分:1)
https://github.com/bilobatum/ActivityIndicatorDemo
使按钮的固有内容大小沿所需的水平轴(即优先级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
答案 1 :(得分:0)
如果您使用自动布局,请考虑在禁用UIActivityIndicatorView时将其设置为0。这不仅仅是你所需要的,但它已经接近了。
答案 2 :(得分:0)
我们假设您的UILabel
被称为_label
,UIProgressView
被称为_progressView
而UIActivityIndicator
被称为_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”功能: