ProgressView溢出UIView边界

时间:2015-01-26 04:00:52

标签: objective-c swift uiview

我在UIView中有一个progressView(UIView),它在边界上流动(使用autolayout)。

  • 当我将进度设置为0.5(50%)时,它将涵盖整个视图
  • 当我将进度设置为0.75(75%)时,它会过度流动UIView

我甚至尝试过sizeToFit()clipToBounds()。都没有解决问题。当我打印帧的宽度时,它是Storyboard中的原始值,当在设备上呈现时它太大了。

class ProgressView: UIView {
    var progress: CGFloat! = 0.75
    var filledView: UIView!
    override init(frame: CGRect) {
        filledView = UIView(frame: CGRect(x: frame.origin.x, y: frame.origin.y, width: 0, height: frame.height))
        filledView.backgroundColor = UIColor.redColor()
        super.init(frame: frame)
        addSubview(filledView)
    }
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        filledView = UIView(frame: CGRect(x: frame.origin.x, y: frame.origin.y, width: 0, height: frame.height))
        filledView.backgroundColor = UIColor.redColor()
        addSubview(filledView)
    }

    func setProgress(var progress: CGFloat) {
        progress = min(max(progress, 0), 1) // Between 0 and 1
        self.progress = progress
        println(self.frame.width)
        filledView.frame.size.width = self.frame.width * progress
        filledView.clipsToBounds = true
    }
}

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

您只需要设置进度:

self.progress = progress

它应该自己动画。 无需更改任何帧。

相关问题