具有圆angular和边框的UIView具有错误的边缘颜色

我有一个UIView和两个子视图。 子视图有圆angular和边框值。 我遇到的问题是圆angular边框的外边缘包含子视图背景色的细线。 我肯定错过了什么??

UIView *outerView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 320)]; [self.view addSubview:outerView]; outerView.backgroundColor = [UIColor whiteColor]; UIView *innerView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 320)]; [outerView addSubview:innerView1]; innerView1.backgroundColor = [UIColor blackColor]; innerView1.layer.borderWidth = 20; innerView1.layer.borderColor = [UIColor whiteColor].CGColor; innerView1.layer.cornerRadius = 20; //innerView1.layer.masksToBounds = YES; UIView *innerView2 = [[UIView alloc] initWithFrame:CGRectMake(160, 0, 160, 320)]; [outerView addSubview:innerView2]; innerView2.backgroundColor = [UIColor blackColor]; innerView2.layer.borderWidth = 20; innerView2.layer.borderColor = [UIColor whiteColor].CGColor; innerView2.layer.cornerRadius = 20; //innerView2.layer.masksToBounds = NO; //innerView2.clipsToBounds = YES; //innerView2.layer.shouldRasterize = YES; 

要解决此问题,请将子视图的背景颜色设置为clearColor ,然后使用自定义视图类的drawRect方法绘制背景颜色。 这是视图类的代码。

 @interface WorkAroundView : UIView @end @implementation WorkAroundView - (void)drawRect:(CGRect)rect { CGFloat margin = self.layer.borderWidth; CGRect background; background.origin.x = margin; background.origin.y = margin; background.size.width = self.bounds.size.width - 2 * margin; background.size.height = self.bounds.size.height - 2 * margin; CGContextRef context = UIGraphicsGetCurrentContext(); [[UIColor blackColor] set]; CGContextFillRect( context, background ); } @end 

这里是你将如何使用自定义视图类。 从你发布的内容来看,唯一真正的变化是子视图的背景颜色被设置为clearColor。

 UIView *outerView = [[UIView alloc] initWithFrame:CGRectMake(360, 200, 320, 320)]; [self.view addSubview:outerView]; outerView.backgroundColor = [UIColor whiteColor]; WorkAroundView *innerView1 = [[WorkAroundView alloc] initWithFrame:CGRectMake(0, 0, 160, 320)]; innerView1.backgroundColor = [UIColor clearColor]; innerView1.layer.borderWidth = 20; innerView1.layer.borderColor = [UIColor whiteColor].CGColor; innerView1.layer.cornerRadius = 20; [outerView addSubview:innerView1]; WorkAroundView *innerView2 = [[WorkAroundView alloc] initWithFrame:CGRectMake(160, 0, 160, 320)]; innerView2.backgroundColor = [UIColor clearColor]; innerView2.layer.borderWidth = 20; innerView2.layer.borderColor = [UIColor whiteColor].CGColor; innerView2.layer.cornerRadius = 20; [outerView addSubview:innerView2]; 

在拐angular处增加一条贝塞尔path

 CAShapeLayer *subLayer = [[CAShapeLayer alloc] init]; [subLayer setFillColor:[UIColor clearColor].CGColor]; [subLayer setStrokeColor:[UIColor whiteColor].CGColor]; [subLayer setLineWidth:1.0]; [subLayer setPath:[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.layer.cornerRadius].CGPath]; [imageView.layer addSublayer:subLayer];