[objective-c]UITextField 不舍入边界

发布时间: 2016/10/4 1:18:09
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我用这个来圆顶两个角的我的 UITextFeild

 CAShapeLayer * maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: self.emailFeild.bounds byRoundingCorners: UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii: (CGSize){10.0, 10.}].CGPath;

我也是有边界添加到为此 UITextField

 self.emailFeild.layer.borderColor = [[self colorFromHexString:@"0x3263A3"]CGColor];
self.emailFeild.layer.borderWidth = 1.0f;
self.emailFeild.layer.mask = maskLayer;

但当我运行它。它不做圆角边框然后这个结果enter image description here

解决方法 1:

遮罩层不被吸引,只是用于计算掩码。请尝试︰

-(void)roundCorners:(UIRectCorner)corners radius:(CGFloat)radius
{
    CGRect bounds = self.bounds;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds
                                                   byRoundingCorners:corners
                                                         cornerRadii:CGSizeMake(radius, radius)];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = bounds;
    maskLayer.path = maskPath.CGPath;

    self.layer.mask = maskLayer;

    CAShapeLayer*   frameLayer = [CAShapeLayer layer];
    frameLayer.frame = bounds;
    frameLayer.path = maskPath.CGPath;
    frameLayer.strokeColor = [UIColor redColor].CGColor;
    frameLayer.fillColor = nil;

    [self.layer addSublayer:frameLayer];
}

-(void)roundTopCornersRadius:(CGFloat)radius
{
    [self roundCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) radius:radius];
}
官方微信
官方QQ群
31647020