如何使CAShapeLayer快速处理约束? [英] How to get CAShapeLayer to work with constraints with swift?
本文介绍了如何使CAShapeLayer快速处理约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在viewdidload中得到了
i got this in viewdidload
rectShape1 = CAShapeLayer()
rectShape1.fillColor = UIColor.blueColor().CGColor
rectShape1.path = UIBezierPath(roundedRect: rectShape1.bounds, byRoundingCorners: .BottomLeft | .TopRight, cornerRadii: CGSize(width: 20, height: 20)).CGPath
redview.layer.addSublayer(rectShape1)
var constTop = NSLayoutConstraint(item: redview, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
view.addConstraint(constTop)
var constH = NSLayoutConstraint(item: redview, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 50)
redview.addConstraint(constH)
var constW = NSLayoutConstraint(item: redview, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 50)
redview.addConstraint(constW)
constH = NSLayoutConstraint(item: redview, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
view.addConstraint(constH)
rectShape1.frame = redview.bounds
,这在didlayoutsubviews中
and this in didlayoutsubviews
self.rectShape1.frame = self.redview.bounds
我在gradientLayer上也有类似的东西,但我工作得很好,
i have similar stuff with gradientLayer and i works fine, any suggestions?
推荐答案
问题是 rectShape1.bounds.size
创建贝塞尔曲线路径时的值为{0,0}。从 viewDidLoad
中删除创建路径的行,并在 viewDidLayoutSubviews
,
The problem is that rectShape1.bounds.size
is {0,0} at the time you create the bezier path. Remove the line where you create the path from viewDidLoad
, and move it to after you set the frame in viewDidLayoutSubviews
,
override func viewDidLayoutSubviews() {
self.rectShape1.frame = self.redview.bounds
rectShape1.path = UIBezierPath(roundedRect: rectShape1.bounds, byRoundingCorners: .BottomLeft | .TopRight, cornerRadii: CGSize(width: 20, height: 20)).CGPath
}
这篇关于如何使CAShapeLayer快速处理约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文