![](/img/trans.png)
[英]Why doesn't setting backgroundColor have a visible effect on my custom UIView?
[英]In my custom UIView, setting a subview's backgroundColor causes it to cover up my drawRect drawings (CGContextRef, CGMutablePathRef,etc). How to fix?
我有一個帶有橙色backgroundColor
的自定義容器視圖。
在此自定義視圖中,我通過重寫drawRect
繪制線條。 除非我嘗試在子視圖上畫線,否則這將非常有用。
以下是一些與代碼編輯相關的屏幕截圖,以說明我面臨的問題:
^此圖顯示了具有
self.graphBackgroundView
自定義視圖,但未設置顯式的backgroundColor
。 我的drawRect
是可見的。 很好
^此圖顯示了我的自定義視圖,其中存在
self.graphBackgroundView
,但是設置了顯式的backgroundColor
。 好像我的綠色子視圖的z-index高於我的drawRect的z-index一樣。
^最后,此圖像顯示了我的自定義視圖,其中存在
self.graphBackgroundView
,已設置了顯式backgroundColor
(仍為綠色),但self.graphBackgroundView.layer.opacity
設置為0.25。 在這里,我們可以再次看到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;
}
好像我的綠色子視圖的z-index高於我的drawRect的z-index一樣。
好吧,確實如此。 如果您有一個超級視圖及其子視圖,則這些子視圖會繪制在該超級視圖的前面 。 這就是成為子視圖的一部分。 這里唯一的驚喜是您很驚訝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.