我有一个CALayer,我是一个圆形图像,我正在尝试为此添加一个外部边框。 (阴影在这里对我不起作用)。我尝试调整CALAyer的大小并添加边框,但是定位似乎不正确,我不希望形状移动,仅添加边框。
这是我的代码
CGFloat borderWidth = 10.0f;
CGFloat xPos = cont.layer.frame.origin.x;
CGFloat yPos = cont.layer.frame.origin.y;
CGFloat contWidth = cont.layer.frame.size.width;
CGFloat contHeight = cont.layer.frame.size.width;
cont.layer.frame = CGRectMake(xPos, yPos, contWidth + borderWidth, contHeight + borderWidth);
cont.layer.cornerRadius = contWidth / 2;
cont.layer.borderColor = [[UIColor darkGrayColor] CGColor];
cont.layer.borderWidth = borderWidth;
提前致谢
最佳答案
您的代码是正确的。
请添加一行:
cont.clipsToBounds =是
self.imgView1.layer.cornerRadius = self.imgView1.frame.size.width / 2;
self.imgView1.layer.borderColor = [[UIColor blueColor] CGColor];
self.imgView1.layer.borderWidth = 0.0f;
self.imgView1.layer.masksToBounds = TRUE;
之后,请按照下列步骤操作:
并编写以下代码:
self.imgView2.layer.cornerRadius = self.imgView2.frame.size.width / 2;
self.imgView2.layer.borderColor = [[UIColor blueColor] CGColor];
self.imgView2.layer.borderWidth = 10.0f;
self.imgView2.layer.masksToBounds = TRUE;
请执行上述步骤,它将解决您的问题。
现在您将获得如下输出:
关于ios - CALayer上的边框显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29645629/