简体   繁体   中英

ProgressView Overflowing UIView bounds

I have a progressView (UIView) inside a UIView, and it over flows the bounds (using autolayout).

  • When I set progress to 0.5 (50%), it covers the entire view
  • When I set progress to 0.75 (75%), it over flows the UIView

I have even tried sizeToFit() and clipToBounds() . Neither have solved the problem. When I println the width of the frame, it is the original value in Storyboard, which is too big when rendered on the device.

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
    }
}

在此处输入图片说明在此处输入图片说明

You just need to set the progress with:

self.progress = progress

It should animate by itself. No need to change any frames in there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM