Reputation: 690
I need to round specific corner of my image view and uiview Imgview Top left and Top right View Bottom left and Bottom right.
and I explore and find this method
[self setMaskTo:_viewBookNow byRoundingCorners:UIRectCornerBottomLeft];
[self setMaskTo:_viewBookNow byRoundingCorners:UIRectCornerBottomRight|UIRectCornerBottomRight];
[self setMaskTo:_imgVAnimal byRoundingCorners:UIRectCornerTopRight];
[self setMaskTo:_imgVAnimal byRoundingCorners:UIRectCornerTopLeft];
Method definition is this
- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
byRoundingCorners:corners
cornerRadii:CGSizeMake(8.0, 8.0)];
CAShapeLayer *shape = [[CAShapeLayer alloc] init];
[shape setPath:rounded.CGPath];
view.layer.mask = shape;
}
but it only round left corners of view and image not work for right corners.
Please suggest
Thanks
Upvotes: 0
Views: 515
Reputation: 1421
Objective C Solution -
- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
byRoundingCorners:corners
cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *shape = [[CAShapeLayer alloc] init];
[shape setPath:rounded.CGPath];
view.layer.mask = shape;
}
And use it like this -
[self setMaskTo:viewToRound byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
Swift Solution -
Please create this extension in your file first
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
}
}
Then create a custom subclass of the UIView and write these lines in that class
override func layoutSubviews() {
super.layoutSubviews()
self.roundCorners([.topLeft, .bottomRight], radius: 50)
}
Set those specific corners in the array as per your need.
Also set the clipToBounds = true in the storyboard or programatically.
Upvotes: 0