2016-12-09 2 views
0

사용자 정의 컨테이너보기에 주황색 backgroundColor이 있습니다.내 사용자 지정 UIView에서 하위 뷰의 backgroundColor를 설정하면 내 drawRect 드로잉 (CGContextRef, CGMutablePathRef 등)을 덮게됩니다. 어떻게 고치는 지?

이 사용자 정의보기에서는 drawRect을 무시하여 선을 그립니다. 이것은 서브 뷰 위에 선을 그릴 때까지 훌륭하게 작동합니다. 여기

내가 직면하고있어 문제를 설명하기 위해 편집 코드와 상관 관계가 스크린 샷입니다 :

enter image description here 이 ^이 이미지는 self.graphBackgroundView 현재 내 사용자 정의보기를 표시하지만 명시 적 backgroundColor없이 설정된다. 내 drawRect의 내 줄을 볼 수 있습니다. 이것은 좋다.

enter image description here ^이 이미지는 self.graphBackgroundView 인 사용자 정의보기를 표시하지만 명시 적으로 backgroundColor이 설정되어 있습니다. 녹색 초록의 z- 색인이 내 drawRect의 z- 색인보다 높거나 같습니다.

enter image description here ^가 마지막이 이미지는 본 self.graphBackgroundView 내 정의보기를 도시 명시 backgroundColor가 설정되고 (여전히 녹색)하지만 0.25의 self.graphBackgroundView.layer.opacity 세트. 여기서 우리는 drawRect 라인을 다시 볼 수 있습니다. 그러나 그것은 옳지 않습니다. 라인이 뷰의 맨 위에 그려지기를 바랄뿐입니다.

실제 문제는 녹색 스크린 샷에서 볼 수 있습니다. 우리는 불투명 한 녹색 하위 뷰를 원합니다. 우리는 단지 흰색 선을 그 위에 그린 싶습니다.

많은 도움을 주셨습니다.

- (void)drawRect:(CGRect)rect 
{ 
    // GET CONTEXT 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // INIT PATH 
    CGMutablePathRef path = CGPathCreateMutable(); 

    // CONFIG PATH 
    CGContextSetLineWidth(context, 1.0); 
    CGContextSetStrokeColor(context, CGColorGetComponents([UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor)); // 4 color component white for use with CGColorGetComponents 

    // ESTABLISH STARTING POINT 
    CGPathMoveToPoint(path, NULL, 0.0, 100.0); 

    // GRAPH NEXT POINT 
    CGPathAddQuadCurveToPoint(path, NULL, 120.0, 160.0, 120.0, 160.0); 

    // GRAPH NEXT POINT 
    CGPathAddQuadCurveToPoint(path, NULL, 130.0, 170.0, 130.0, 170.0); 

    // ADD THE PATH 
    CGContextAddPath(context, path); 

    // DRAW 
    CGContextDrawPath(context, kCGPathStroke); 
} 

- (id)init 
{ 
    self = [super init]; 
    if (self) 
    { 
     // INIT UI ELEMENTS 
     self.graphBackgroundView = [[UIView alloc] init]; 

     // INIT AUTO LAYOUT VIEWS DICT 
     self.viewsDictionary = [NSMutableDictionary new]; 
     [self.viewsDictionary setObject:self.graphBackgroundView forKey:@"graphBackgroundView"]; 

     // TURN ON AUTO LAYOUT 
     self.graphBackgroundView.translatesAutoresizingMaskIntoConstraints = NO; 

     // ESTABLISH VIEW HIERARCHY 
     [self addSubview:self.graphBackgroundView]; 

     // LAYOUT 

     // graphBackgroundView 
     [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0)-[graphBackgroundView]-(0)-|" options:0 metrics:0 views:self.viewsDictionary]]; 
     [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(topMargin)-[graphBackgroundView]-(bottomMargin)-|" options:0 metrics:@{@"topMargin":self.topMargin,@"bottomMargin":self.bottomMargin} views:self.viewsDictionary]]; 

     // CONFIG 

     // self 
     self.backgroundColor = [UIColor orangeColor]; // My drawRect code DOES draw on top of this color 

     // graphBackgroundView 
     self.graphBackgroundView.backgroundColor = [UIColor greenColor]; // My drawRect code does NOT draw on top of this color 
     //self.graphBackgroundView.layer.opacity = 0.25; // <-- If I uncomment this I can kind of see the line I'm drawing underneath it via the effects of transparency 
    } 
    return self; 
} 

답변

0

그것은 내 녹색 서브 뷰의 Z- 인덱스 내의 drawRect의 Z- 인덱스, 또는 무언가보다 높은 경우로이다 :

여기에 코드입니다.

글쎄, 그렇지. 수퍼 뷰 및 해당 하위 뷰가있는 경우 하위 뷰는 수퍼 뷰 앞에 으로 그려집니다. 이것이 하위보기라는 것을 의미합니다. 놀라운 사실은 놀랐다는 것입니다.

+0

코드를 작성하는 시점에 나는 사용자 정의보기를 "단일 한 것"으로 생각했습니다. 마찬가지로,'drawRect'는 자신과 서브 뷰를 그리기 위해 돌았습니다. ('init'에서 나는 그것들을 레이아웃하고 있습니다 ...). 예,보기, 내부 드로잉 코드 등을 생각할 때 이것이 의미가없는 방식을 지금 보았습니다. 어쨌든, 이제 알아 냈습니다. 당신의 대답은 들썩 들썩합니다. 그것은 여기서 무슨 일이 일어나는지 정확히 설명한다는 점에서 매우 도움이됩니다. 건배, 고마워! –

관련 문제