ios - CALayer上的边框显示不正确

标签 ios objective-c xcode border calayer

我有一个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;

之后,请按照下列步骤操作:

  • 创建另一个UIImageView并将其放置在第一个imageView上方。
  • 然后将secondImageView的高度和宽度增加到20。
  • 将secondImageView中心和FirstImageCenter设置为相等(在同一点)。

  • 并编写以下代码:

    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/

    相关文章:

    objective-c - 从 NSTextField 类别内访问 View ?

    ios - UITableView上的垂直手势识别

    ios - xcode 在生成 ipa 文件时退出

    xcode - 如何删除 Xcode 9 中标记单词上插入的双引号

    ios - 如何隐藏 UIContextMenuInteraction/UITargetedPreview 黑色背景?

    ios - 没有网络时如何快速阻止 ios,有什么最佳实践吗?

    ios - Iphone 5 应用程序不适合 Iphone 4

    objective-c - 为什么这是 CTFrame 的过度发布

    ios - Swift addTarget 到一个按钮并调用来自 UIview 文件的不同 View Controller 文件中的函数

    iOS 7 在导航的自定义动画中显示黑色背景