使用uibezierpath在UIView上绘制边框

Moh*_*aji -1 border objective-c uiview ios

我需要你帮助使用uibezierpath或类似的方法在UIView上绘制这个边框.

在此输入图像描述

我已经做了这个解决方案:在另一个中嵌入一个UIView,但我认为有更好的解决方案:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    self.theView.layer.cornerRadius = self.theView.bounds.size.width/2;
    self.theView.layer.masksToBounds = YES;

    UIView *borderView = [[UIView alloc] initWithFrame:CGRectMake(4, 4, CGRectGetWidth(self.theView.frame) - 8, CGRectGetHeight(self.theView.frame) - 8)];
    borderView.backgroundColor = [UIColor grayColor];
    borderView.layer.cornerRadius = self.theView.bounds.size.width/2;
    borderView.layer.masksToBounds = YES;

    self.theView.layer.borderWidth = 2;
    self.theView.layer.borderColor = [UIColor redColor].CGColor;
    self.theView.backgroundColor = [UIColor clearColor];
    [self.theView addSubview:borderView];

}
Run Code Online (Sandbox Code Playgroud)

谢谢.

Tej*_*uri 5

您可以UIView使用两个贝塞尔曲线路径将其绘制成一个.UIView对该UIView drawRect方法进行子类化并将其包括在内,

-(void)drawRect:(CGRect)frame
{
    UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(CGRectGetMinX(frame) + 2, CGRectGetMinY(frame) + 2.5, CGRectGetWidth(frame) - 5, CGRectGetHeight(frame) - 5)];
    [UIColor.redColor setStroke];
    ovalPath.lineWidth = 1;
    [ovalPath stroke];

   UIBezierPath* oval2Path = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(CGRectGetMinX(frame) + 10, CGRectGetMinY(frame) + 10, CGRectGetWidth(frame) - 20, CGRectGetHeight(frame) - 20)];
    [UIColor.lightGrayColor setFill];
    [oval2Path fill];
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述