2016-07-19 12 views
-1

uibezierpathなどを使用して、このボーダーをUIViewに描画するのに役立ちます。uibezierpathを使用してUIViewに境界線を描画する

enter image description here

私は、このソリューション行った:別のものに巣の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]; 

} 

おかげで。

答えて

4

UIViewの2つのベジェパスを使用して1つに描画できます。 、UIViewをサブクラス化UIViewdrawRect方法でこれを含む

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

enter image description here

関連する問題