在UITextField / UIView中只有一个角落被舍入

时间:2015-08-04 12:45:29

标签: ios objective-c uitextfield

我使用以下代码使UITextField' s / UIView的顶角四舍五入,但问题只是左上角是圆角。我做错了什么或者还有其他方法可以做到。

- (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;
    }
}

调用上述方法

[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.

3 个答案:

答案 0 :(得分:1)

我认为代码没有错。右侧的其他一些视图与文本字段重叠。这让您感觉右角不是圆角。通过将所有四个角都变为圆形来检查相同的东西,您将能够识别相同的内容。

答案 1 :(得分:0)

对于单个角:

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

对于多个角:

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

答案 2 :(得分:-1)

试试这个

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];

希望它有所帮助。

检查此链接

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

希望它有所帮助。