2014-04-15 89 views
6

我有一個UIView和兩個子視圖。子視圖具有圓角和邊框值。我遇到的問題是圓角邊框的外邊緣包含子視圖背景顏色的細線。我肯定錯過了什麼??具有圓角和邊框的UIView有錯誤的邊緣顏色

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; 
+0

這將有助於包括顯示這一問題的相關截圖貝塞爾曲線路徑。 – rmaddy

+0

我用綠色背景運行了你的代碼,這就是我所得到的 - > http://puu.sh/89bjO/6e551e085e.png就是你所得到的?我沒有真正將你的問題與我得到的相匹配 –

+0

我現在看到你的錯誤。我搞亂了代碼,看看造成這種情況的原因。 –

回答

2

要解決的問題,將子視圖的背景顏色設置爲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]; 
+0

這是一個很好的答案 – Legolas

1

添加指日可待

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