UIProgressView在iOS 7中显示问题

UIProgressView在iOS 7中显示问题,ios,iphone,objective-c,ios7,uiprogressview,Ios,Iphone,Objective C,Ios7,Uiprogressview,我需要UIProgressView作为一个iOS 7应用程序的UITableView单元的一部分。我在单元格右侧的代码中添加了UIProgressView,如下所示: _progressBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; _progressBar.translatesAutoresizingMaskIntoConstraints = NO; [self.conte

我需要UIProgressView作为一个iOS 7应用程序的UITableView单元的一部分。我在单元格右侧的代码中添加了UIProgressView,如下所示:

_progressBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
_progressBar.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:_progressBar];

[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_progressBar attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_progressBar attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.0]];
[_progressBar addConstraint:[NSLayoutConstraint constraintWithItem:_progressBar attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:60.0]];
[_progressBar addConstraint:[NSLayoutConstraint constraintWithItem:_progressBar attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:12.0]];
然而,显示UIProgressView有点奇怪。当进度为零时,UIProgressView对象如下所示:

_progressBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
_progressBar.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:_progressBar];

[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_progressBar attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_progressBar attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.0]];
[_progressBar addConstraint:[NSLayoutConstraint constraintWithItem:_progressBar attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:60.0]];
[_progressBar addConstraint:[NSLayoutConstraint constraintWithItem:_progressBar attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:12.0]];

当我将进度值设置为0.5时,UIProgressView左侧变为矩形,右侧保持圆角矩形:

当我将进度设置为0.98时,如下所示:

_progressBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
_progressBar.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:_progressBar];

[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_progressBar attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_progressBar attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.0]];
[_progressBar addConstraint:[NSLayoutConstraint constraintWithItem:_progressBar attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:60.0]];
[_progressBar addConstraint:[NSLayoutConstraint constraintWithItem:_progressBar attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:12.0]];

您知道如何通过简单地设置进度值来填充UIProgressView,并且始终保持元素的圆角矩形(或矩形)形式吗?或者,如果不添加自定义图形或覆盖UIProgressView,这是不可能的


非常感谢您,并致以最良好的问候。

今天遇到了同样的问题,解决问题并不难:)

在interface builder中,添加与UIProgressView大小完全相同的UIView。在此新UIView中添加UIProgressView

如果需要角点,请对UIView执行以下操作:

1) 将拐角半径设置为高度的一半(例如,30px高度进度条表示15px拐角半径UIVIew))

2) 将掩码设置为边界

myView.layer.masksToBounds = TRUE;
3) 将剪辑设置为边界,这样就不会有任何内容从UIView中显示出来

myView.clipsToBounds = TRUE;
现在你有了一个圆形的,你想要的高度

如果不需要圆角,只需以相同的方式添加UIView,但只需将背景颜色更改为与UIProgressbar tracktint相同的灰色

祝你好运


-Sjoerd

供将来参考:我已通过将拐角半径设置为

  _progressView.layer.cornerRadius = 2.0;
然后:

  _progressView.clipsToBounds = YES;

UIProgressView不需要容器。

我认为UIProgressView的高度是固定的。progressview的高度可以更改,使用CGAffineTransform会更容易:将
UIView
的背景更改为
clearColor
是否
clipsToBounds
masksToBounds
相同?