2014-04-15 16 views
6

Ho un UIView e due sotto-visualizzazioni in esso. Le sottoview hanno angoli arrotondati e un valore di confine. Il problema che ho è che i bordi esterni del bordo arrotondato contengono una linea sottile del colore di sfondo subview. Devo mancare qualcosa ??UIView con angolo arrotondato e bordo ha il colore del bordo sbagliato

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

Sarebbe utile includere uno screenshot che mostra il rilevante problema. – rmaddy

+0

Ho eseguito il codice con uno sfondo verde e questo è quello che sto ottenendo -> http://puu.sh/89bjO/6e551e085e.png è quello che stai ottenendo? Non sto davvero confrontando il tuo problema con quello che sto ricevendo –

+0

Ora vedo il tuo errore. Sto scherzando con il codice per vedere cosa sta causando questo. –

risposta

2

Per aggirare il problema, impostare il colore delle subviews sfondo per clearColor e quindi disegnare il colore di sfondo utilizzando il metodo di una classe visualizzazione personalizzata drawRect. Ecco il codice per la classe vista.

@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 

Ed ecco come si utilizzerà la classe di visualizzazione personalizzata. L'unico vero cambiamento qui da ciò che hai postato è che il colore di sfondo per le subviews è impostato su 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

Questa è un'ottima risposta – Legolas

1

aggiungere un tracciato Bezier dietro l'angolo

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