迅速:更改UIView的大小

时间:2018-08-15 08:11:52

标签: ios swift

我使用此类创建循环进度:

class ProgressBarView: UIView {

    var bgPath: UIBezierPath!
    var shapeLayer: CAShapeLayer!
    var progressLayer: CAShapeLayer!

    var progress: Float = 0 {
        willSet(newValue)
        {
            progressLayer.strokeEnd = CGFloat(newValue)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        bgPath = UIBezierPath()
        self.simpleShape()
    }

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        bgPath = UIBezierPath()
        self.simpleShape()
    }

    func simpleShape()
    {
        createCirclePath()
        shapeLayer = CAShapeLayer()
        shapeLayer.path = bgPath.cgPath
        shapeLayer.lineWidth = 2.5
        shapeLayer.fillColor = nil
        shapeLayer.strokeColor = UIColor.black.cgColor.copy(alpha: 0.2)

        progressLayer = CAShapeLayer()
        progressLayer.path = bgPath.cgPath
        progressLayer.lineCap = kCALineCapRound
        progressLayer.lineWidth = 2.5
        progressLayer.fillColor = nil
        progressLayer.strokeColor = UIColor(red: 0/255, green: 122/255, blue: 255/255, alpha: 1.0).cgColor
        progressLayer.strokeEnd = 0.0


        self.layer.addSublayer(shapeLayer)
        self.layer.addSublayer(progressLayer)
    }

    private func createCirclePath()
    {

        let x = self.frame.width/2
        let y = self.frame.height/2
        let center = CGPoint(x: x, y: y)
        print(x,y,center)
        bgPath.addArc(withCenter: center, radius: x, startAngle: CGFloat(11), endAngle: CGFloat(17.28), clockwise: true)
        bgPath.close()
    }
}

在我的ViewController中,我将此类用于progressView。我想为iPhone SE更改progressView的大小。我尝试在情节提要中创建宽度约束,并以编程方式更改progressView的大小,例如:

if self.view.frame.height == 568 {

    progressViewWidthConstraint.constant = 100
}

但是它不起作用。如何更改iPhone SE的尺寸?

更新

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要重新布局

progressViewWidthConstraint.constant = 100
self.view.layoutIfNeeded()