How To Set Rounded Corner on View in Swift 4?
This tutorial to set corner radius on view, Round Left , Round Right , Round Top , and Round Bottom.
Create the extension on view.
bottomRounded on UIView.
Corner on View :
Thanks.
Create the extension on view.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384extension UIView{
func roundedTopLeft(){
let maskPath1 = UIBezierPath(roundedRect: bounds,
byRoundingCorners: [.topLeft],
cornerRadii: CGSize(width: 15, height: 15))
let maskLayer1 = CAShapeLayer()
maskLayer1.frame = bounds
maskLayer1.path = maskPath1.cgPath
layer.mask = maskLayer1
}
func roundedTopRight(){
let maskPath1 = UIBezierPath(roundedRect: bounds,
byRoundingCorners: [.topRight],
cornerRadii: CGSize(width: 15, height: 15))
let maskLayer1 = CAShapeLayer()
maskLayer1.frame = bounds
maskLayer1.path = maskPath1.cgPath
layer.mask = maskLayer1
}
func roundedBottomLeft(){
let maskPath1 = UIBezierPath(roundedRect: bounds,
byRoundingCorners: [.bottomLeft],
cornerRadii: CGSize(width: 15, height: 15))
let maskLayer1 = CAShapeLayer()
maskLayer1.frame = bounds
maskLayer1.path = maskPath1.cgPath
layer.mask = maskLayer1
}
func roundedBottomRight(){
let maskPath1 = UIBezierPath(roundedRect: bounds,
byRoundingCorners: [.bottomRight],
cornerRadii: CGSize(width: 15, height: 15))
let maskLayer1 = CAShapeLayer()
maskLayer1.frame = bounds
maskLayer1.path = maskPath1.cgPath
layer.mask = maskLayer1
}
func roundedBottom(){
let maskPath1 = UIBezierPath(roundedRect: bounds,
byRoundingCorners: [.bottomRight , .bottomLeft],
cornerRadii: CGSize(width: 15, height: 15))
let maskLayer1 = CAShapeLayer()
maskLayer1.frame = bounds
maskLayer1.path = maskPath1.cgPath
layer.mask = maskLayer1
}
func roundedTop(){
let maskPath1 = UIBezierPath(roundedRect: bounds,
byRoundingCorners: [.topRight , .topLeft],
cornerRadii: CGSize(width: 15, height: 15))
let maskLayer1 = CAShapeLayer()
maskLayer1.frame = bounds
maskLayer1.path = maskPath1.cgPath
layer.mask = maskLayer1
}
func roundedLeft(){
let maskPath1 = UIBezierPath(roundedRect: bounds,
byRoundingCorners: [.topLeft , .bottomLeft],
cornerRadii: CGSize(width: 15, height: 15))
let maskLayer1 = CAShapeLayer()
maskLayer1.frame = bounds
maskLayer1.path = maskPath1.cgPath
layer.mask = maskLayer1
}
func roundedRight(){
let maskPath1 = UIBezierPath(roundedRect: bounds,
byRoundingCorners: [.topRight , .bottomRight],
cornerRadii: CGSize(width: 15, height: 15))
let maskLayer1 = CAShapeLayer()
maskLayer1.frame = bounds
maskLayer1.path = maskPath1.cgPath
layer.mask = maskLayer1
}
func roundedAllCorner(){
let maskPath1 = UIBezierPath(roundedRect: bounds,
byRoundingCorners: [.topRight , .bottomRight , .topLeft , .bottomLeft],
cornerRadii: CGSize(width: 15, height: 15))
let maskLayer1 = CAShapeLayer()
maskLayer1.frame = bounds
maskLayer1.path = maskPath1.cgPath
layer.mask = maskLayer1
}
}
bottomRounded on UIView.
1234567891011override func viewDidLoad() {
let topRight = UIView(frame: CGRect(x: 120, y: 200, width: 120, height: 120))
// Change here the function to set corner radius.
topRight.roundedBottom()
topRight.backgroundColor = .red
self.view.center = topRight.center
self.view.addSubview(topRight)
super.viewDidLoad()
}
topRound Corner on View.12345678override func viewDidLoad() {
let topRight = UIView(frame: CGRect(x: 120, y: 200, width: 120, height: 120))
topRight.roundedTop()
topRight.backgroundColor = .red
self.view.center = topRight.center
self.view.addSubview(topRight)
super.viewDidLoad()
}
leftRound Corner on View:12345678override func viewDidLoad() {
let topRight = UIView(frame: CGRect(x: 120, y: 200, width: 120, height: 120))
topRight.roundedLeft()
topRight.backgroundColor = .red
self.view.center = topRight.center
self.view.addSubview(topRight)
super.viewDidLoad()
}
RightRound Corner on View :12345678override func viewDidLoad() {
let topRight = UIView(frame: CGRect(x: 120, y: 200, width: 120, height: 120))
topRight.roundedRight()
topRight.backgroundColor = .red
self.view.center = topRight.center
self.view.addSubview(topRight)
super.viewDidLoad()
}
topRight Corner on View :12345678override func viewDidLoad() {
let topRight = UIView(frame: CGRect(x: 120, y: 200, width: 120, height: 120))
topRight.roundedTopRight()
topRight.backgroundColor = .red
self.view.center = topRight.center
self.view.addSubview(topRight)
super.viewDidLoad()
}
topLeft Corner on View :12345678override func viewDidLoad() {
let topRight = UIView(frame: CGRect(x: 120, y: 200, width: 120, height: 120))
topRight.roundedTopLeft()
topRight.backgroundColor = .red
self.view.center = topRight.center
self.view.addSubview(topRight)
super.viewDidLoad()
}
bottomLeft Corner on View :12345678override func viewDidLoad() {
let topRight = UIView(frame: CGRect(x: 120, y: 200, width: 120, height: 120))
topRight.roundedBottomLeft()
topRight.backgroundColor = .red
self.view.center = topRight.center
self.view.addSubview(topRight)
super.viewDidLoad()
}
bottomRight Corner on View :12345678override func viewDidLoad() {
let topRight = UIView(frame: CGRect(x: 120, y: 200, width: 120, height: 120))
topRight.roundedBottomRight()
topRight.backgroundColor = .red
self.view.center = topRight.center
self.view.addSubview(topRight)
super.viewDidLoad()
}
Corner on View :
12345678override func viewDidLoad() {
let topRight = UIView(frame: CGRect(x: 120, y: 200, width: 120, height: 120))
topRight.roundedAllCorner()
topRight.backgroundColor = .red
self.view.center = topRight.center
self.view.addSubview(topRight)
super.viewDidLoad()
}
Thanks.
How To Set Rounded Corner on View in Swift 4?
Reviewed by KIRIT MODI
on
22:00:00
Rating:
