如何在两个重叠的 UIView 周围绘制边框

How to draw a border around two overlapping UIViews

我有两个 UIView,它们在重叠时创建了一个独特的形状,我想在合并的视图周围画一个边框。

这样做的正确方法是什么?

UIView 是:

  1. 圆形图像视图以显示用户个人资料图像
  2. 将包含用户配置文件数据(即姓名、出生日期等)的矩形视图

这是两个视图一起显示的图像:

好的,通过执行以下操作解决了这个问题:

  1. 在矩形 UIView 上设置边框
  2. UIBezierPath创建一个CAShapeLayer画一个半圆,并在drawRect方法中添加到圆UIView's层:

    覆盖 func draw(_ rect: CGRect) { super.draw(矩形)

    let shapeLayer = CAShapeLayer()
    let topSemiCirclePath = UIBezierPath(arcCenter: userImageView.center, radius: userImageView.bounds.size.width / 2.0, startAngle: CGFloat(Double.pi), endAngle: CGFloat(Double.pi / 180), clockwise: true)
    topSemiCirclePath.lineWidth = 2.0
    UIColor.lightGray.setStroke()
    topSemiCirclePath.stroke()
    shapeLayer.path = topSemiCirclePath.cgPath
    userImageView.layer.addSublayer(shapeLayer)
    

    }