0
votes

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

1
Use clipsToBounds = true & maskstobounds = true and trydahiya_boy
Property masks to bounds not for view. showing this error.Chandni
Not working.....Chandni
Use view.layer.masktobounds = true Its a layer property.dahiya_boy

1 Answers

0
votes

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.