I have used below code in UIViewController's viewWillLayoutSubviews() method to add border to UIView.
override func viewWillLayoutSubviews() {
guard let shapeLayer = view.superview?.superview?.mask?.layer as? CAShapeLayer else { return }
let borderLayer = CAShapeLayer()
borderLayer.path = shapeLayer.path
borderLayer.lineWidth = 1
borderLayer.strokeColor = UIColor.init(hexString: "#2B2B2B")?.cgColor
borderLayer.fillColor = UIColor.clear.cgColor
borderLayer.frame = shapeLayer.bounds
view.superview?.superview?.layer.addSublayer(borderLayer)
}
Output screenshot(it's showing multiple border line in iOS17)