-1

I am using the following code to make UITextField's/UIView's Top corners rounded, but the problem is only left top corner is getting rounded. What I am doing wrong or is there any other way to do so.

- (void)roundCornersOnView:(UIView*)view onTopLeft:(BOOL)tl topRight:(BOOL)tr bottomLeft:(BOOL)bl bottomRight:(BOOL)br radius:(float)radius
{

    if (tl || tr || bl || br) {
        UIRectCorner corner = 0; //holds the corner
        //Determine which corner(s) should be changed
        if (tl) {
            corner = corner | UIRectCornerTopLeft;
        }
        if (tr) {
            corner = corner | UIRectCornerTopRight;
        }
        if (bl) {
            corner = corner | UIRectCornerBottomLeft;
        }
        if (br) {
            corner = corner | UIRectCornerBottomRight;
        }

        UIView* roundedView = view;
        UIBezierPath* maskPath = [UIBezierPath bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
        CAShapeLayer* maskLayer = [CAShapeLayer layer];
        maskLayer.frame = roundedView.bounds;
        maskLayer.path = maskPath.CGPath;
        roundedView.layer.mask = maskLayer;
    }
}

Calling the above method

[self roundCornersOnView:textField onTopLeft:YES topRight:YES bottomLeft:NO bottomRight:NO radius:5.0];

As u can see in the screen shot, I have applied the same code in it.

Shubham
  • 88
  • 6

3 Answers3

1

I think there is nothing wrong in the code. Some other view on the right side overlaps the textfield. This gives you a feeling that your right corner is not rounded. Check same thing by making all four corner as round and you will able to identify same.

Apurv
  • 17,116
  • 8
  • 51
  • 67
  • I have attached a screenshot where i applied the same code. I crosschecked there is no other view overlapping the view. – Shubham Aug 05 '15 at 06:32
0

For single corner:

textField.roundCorners(corners: .topLeft, radius: 0.07 * textField.bounds.height)

For mutliple corner:

textField.roundCorners(corners: [.topLeft, .bottomLeft], radius: 0.07 * textField.bounds.height)
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
-1

Try this

UItextField * textField = [[UITextField alloc] initWithFrame:Frame];
textField.delegate = self;
[textField setBackgroundColor:[UIColor whiteColor]];
[textField.layer setBorderColor:[UIColor grayColor].CGColor];
[textField.layer setBorderWidth:1.0];
[textField.layer setCornerRadius:14.0f];
textField.placeholder = @"PlaceHolder";
[self.view textField];

Hope it helps.

Check this link

how to set cornerRadius for only top-left and top-right corner of a UIView?

Hope it helps.

Community
  • 1
  • 1
Pradumna Patil
  • 2,180
  • 3
  • 17
  • 46
  • I need only top corners of the textfield to get rounded not the bottom ones. Will this code fulfil the requirement. – Shubham Aug 04 '15 at 12:53
  • The question is not how to do it but why is the code wrong. Your answer doesn't answer it. – Nat Aug 04 '15 at 13:12