I have to set the corner radius according to the UIView found in the background. I have attached the image of my development for your reference.
Asked
Active
Viewed 1,079 times
1
-
just apply radius to main container view (no need for button) – SPatel Dec 13 '18 at 09:13
-
4Very similar [how to set cornerRadius for only...](https://stackoverflow.com/questions/31232689/how-to-set-cornerradius-for-only-bottom-left-bottom-right-and-top-left-corner-te) – Robert Dresler Dec 13 '18 at 09:14
-
@SPatel I have set the corner radius for the whole view but the button is not changing – KARTHICK T M Dec 13 '18 at 09:17
-
Check https://stackoverflow.com/a/53435299/2025766 – FedeH Dec 13 '18 at 09:31
3 Answers
1
You can try like this.
if #available(iOS 11.0, *) {
customView.layer.cornerRadius = 10
customView.layer.maskedCorners = [.layerMinXMaxYCorner,.layerMinXMinYCorner,.layerMaxXMinYCorner]
} else {
// Fallback on earlier versions
let rectShape = CAShapeLayer()
rectShape.bounds = customView.frame
rectShape.position = customView.center
rectShape.path = UIBezierPath(roundedRect: customView.bounds, byRoundingCorners: [.bottomRight , .topLeft , .topRight], cornerRadii: CGSize(width: 10, height: 10)).cgPath
customView.layer.mask = rectShape
}

Kathiresan Murugan
- 2,783
- 3
- 23
- 44
-
-
-
You can change a code like to set for button corner radius button.layer.maskedCorners = [.layerMaxXMaxYCorner] – Kathiresan Murugan Dec 13 '18 at 10:27
0
Just create custom view like below, and apply mask and path to main layer
@IBDesignable class VariableSeparateCornerView: UIView {
private var corners: UIRectCorner = [.allCorners]
private var radius: CGFloat = 0
override func layoutSubviews() {
super.layoutSubviews()
self.refreshCorners()
}
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
self.corners = corners
self.radius = radius
self.refreshCorners()
}
private func refreshCorners() {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
}
Use:
let containerView = VariableSeparateCornerView()
containerView.roundCorners(corners: [.topRight, .bottomLeft, .bottomRight], radius: 20)

SPatel
- 4,768
- 4
- 32
- 51
0
This is the extension version. It may be easier.
extension UIView {
func roundCorners(_ corners:UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
}
Use:
button?.roundCorners([.topRight, .bottomLeft, .bottomRight], radius: radius)

KingHodor
- 537
- 4
- 17