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)

Not Working in iOS17][1]

Working in iOS16 or lower OS

0

There are 0 best solutions below