【发布时间】:2016-06-08 21:50:16
【问题描述】:
我正在尝试仅舍入视图和图像视图的顶部顶角(TopLeft 和 TopRight)。使用下面的代码,我只能将左角四舍五入(尽管也指定了 TopRight)。也许他没有看到故事板上的约束。
我该如何解决?
谢谢。
let rectShape = CAShapeLayer()
rectShape.bounds = self.CopertinaImage1.frame
rectShape.position = self.CopertinaImage1.center
rectShape.path = UIBezierPath(roundedRect: self.CopertinaImage1.bounds, byRoundingCorners: [.TopRight, .TopLeft], cornerRadii: CGSize(width: 10, height: 10)).CGPath
self.CopertinaImage1.layer.backgroundColor = UIColor.greenColor().CGColor
self.CopertinaImage1.layer.mask = rectShape
我尝试将以下代码放入viewDidLayoutSubviews:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let rectShape = CAShapeLayer()
rectShape.bounds = self.CopertinaImage1.frame
rectShape.position = self.CopertinaImage1.center
rectShape.path = UIBezierPath(roundedRect: self.CopertinaImage1.bounds, byRoundingCorners: [.TopRight, .TopLeft], cornerRadii: CGSize(width: 10, height: 10)).CGPath
self.CopertinaImage1.layer.backgroundColor = UIColor.greenColor().CGColor
self.CopertinaImage1.layer.mask = rectShape
}
但是没有用。
【问题讨论】:
-
如果以前使用自动布局,您需要将代码移动到自动布局引擎完成设置视图框架之后。如果您在
UIViewController中执行此操作,则可能是viewDidLayoutSubviews -
试过了但是不行 我在viewDidLoad下输入了 override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() let rectShape = CAShapeLayer() rectShape.bounds = self.CopertinaImage1.frame rectShape.position = self.CopertinaImage1.center rectShape.path = UIBezierPath(roundedRect: self.CopertinaImage1.bounds, byRoundingCorners: [.TopRight, .TopLeft],cornerRadii: CGSize(width: 10, height: 10)).CGPath self.CopertinaImage1.layer. backgroundColor = UIColor.greenColor().CGColor self.CopertinaImage1.layer.mask = rectShape
-
您试图在
UITableViewCell子类中屏蔽的视图?如果是这样,代码应该在它的layoutSubviews中。rectShape.bounds = self.CopertinaImage1.frame和rectShape.position = self.CopertinaImage1.center在我看来也是错误的。我会尝试用rectShape.frame = self.CopertinaImage1.bounds替换它们 -
没有@beyowulf。我试图通过放置 rectShape.frame = self.CopertinaImage1.bounds 来删除 rectShape.bounds = self.CopertinaImage1.frame 和 rectShape.position = self.CopertinaImage1.center 但它不起作用。 :(
-
这不是表格视图。在视图控制器中,我插入了一个内部(中间)一个 ImageView 的视图。
标签: ios swift rounded-corners cornerradius