2014-04-15 13 views
6

Mam UIView i dwie subviews w nim. Subviews ma zaokrąglone rogi i wartość graniczną. Problem polega na tym, że zewnętrzne krawędzie zaokrąglonej krawędzi zawierają cienką linię koloru tła z podglądu. Muszę czegoś przegapić?UIView z zaokrąglonym rogiem i obramowaniem ma niewłaściwy kolor krawędzi

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

Pomogłoby to obejmować odpowiedni zrzut ekranu przedstawiający problem. – rmaddy

+0

Uruchomiłem twój kod na zielonym tle i to właśnie dostaję -> http://puu.sh/89bjO/6e551e085e.png czy to właśnie dostajesz? Naprawdę nie pasuję do twojego problemu z tym, co dostaję –

+0

Widzę teraz twój błąd. Mam problem z kodem, aby zobaczyć, co to powoduje. –

Odpowiedz

2

Aby obejść ten problem, należy ustawić kolor tła z subviews do clearColor a następnie narysować kolor tła przy użyciu metody klasy niestandardowego widoku drawRect. Oto kod dla klasy widoku.

@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 

Oto sposób użycia niestandardowej klasy widoku. Jedyną prawdziwą zmianą tutaj z tego, co napisałeś, jest to, że kolor tła dla subviews jest ustawiony na 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

To jest świetna odpowiedź – Legolas

1

dodać ścieżkę bezier rogiem

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]; 
Powiązane problemy