IOS / Objective-C:矩形的圆顶角不擦除边框
Posted
技术标签:
【中文标题】IOS / Objective-C:矩形的圆顶角不擦除边框【英文标题】:IOS/Objective-C: Round top corners of rectangle without erasing border 【发布时间】:2017-11-08 17:49:18 【问题描述】:我正在使用以下方法来圆化 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;
【问题讨论】:
【参考方案1】:这样做的一种可能方法是重新绘制边框。作为
-(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
【讨论】:
【参考方案2】:你试过吗?给出一些圆角半径来实现该部分。示例:
nameOfTextField.layer.cornerRadius = 15.0
nameOfTextField.layer.borderWidth = 2.0 // Use border width if you need it
【讨论】:
以上是关于IOS / Objective-C:矩形的圆顶角不擦除边框的主要内容,如果未能解决你的问题,请参考以下文章