IOS/Objective-C: Round top corners of rectangle without erasing border

Viewed 662

I am using the following method to round specific corners of uitextfield. However, it has the effect of erasing the border at the rounded corner. Can anyone suggest way to do this without erasing border? Thanks in advance for any suggestions.

-(void)roundBottomCornersOfView: (UITextField*) view by: (NSInteger) radius {
    CGRect rect = view.bounds;
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect
                                               byRoundingCorners:UIRectCornerTopLeft |UIRectCornerTopRight
                                                     cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *layers = [CAShapeLayer layer];
    layers.frame = rect;
    layers.path = path.CGPath;
    view.layer.mask = layers;
}

enter image description here

2 Answers

One possible way of doing this could be to redraw your border. As

-(void)roundBottomCornersOfView: (UITextField*) view by: (NSInteger) radius {
    CGRect rect = view.bounds;
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect
                         byRoundingCorners:UIRectCornerTopLeft |UIRectCornerTopRight
                         cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *layers = [CAShapeLayer layer];
    layers.frame = rect;
    layers.path = path.CGPath;
    view.layer.mask = layers;

    CAShapeLayer*   borderShape = [CAShapeLayer layer];
    borderShape.frame = rect;
    borderShape.path = path.CGPath;
    borderShape.strokeColor = [UIColor blackColor].CGColor;
    borderShape.fillColor = nil;
    borderShape.lineWidth = 3;
    [view.layer addSublayer:borderShape];
}

Hope this helps. Referred from this SO Post

Have you tried this? Give some corner radius to achieve that part. Example:

nameOfTextField.layer.cornerRadius = 15.0
nameOfTextField.layer.borderWidth = 2.0 // Use border width if you need it
Related