IOS/Objective-C: 矩形的圆顶角没有擦除边框

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

我正在使用以下方法来圆化 uitextfield 的特定角。但是,它具有擦除圆角处边框的效果。任何人都可以建议在不删除边界的情况下执行此操作的方法吗?提前感谢您的任何建议。

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

你试过这个吗?给一些圆角半径来实现那个部分。示例:

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

一种可能的方法是重新绘制边框。作为

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

希望这对您有所帮助。 Referred from this SO Post