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

标签 ios objective-c uitextfield rounded-corners

我正在使用以下方法来圆化 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;
}

enter image description here

最佳答案

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

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

关于IOS/objective-C : Round top corners of rectangle without erasing border,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47186422/

相关文章:

ios - 从不同的 UIViewController 调用方法

ios - CGContextRef画线和点,如何清屏?

ios - 需要将 iOS 5.0 App 转换为 iOS 4.3.3

ios - == 究竟如何与 Set() 一起工作 - Swift

objective-c - NSTableViewDelegate 接收多个 NSNotifications

ios - GKImagePicker 链接器命令失败,退出代码为 1

ios - 如何在 Swift 3 中更改所有 TextField 边框颜色

ios - Swift 中的自动换行标签

iphone - objective-c:如何访问 Tableview 第二部分第一行的文本字段?

ios - UIView 与 uiindicator sethidden = No 在 sethidden = YES 后不起作用