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.
extension 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.
override 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.override 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:override 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 :override 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 :override 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 :override 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 :override 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 :override 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 :
override 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:
No comments: