[英]Changing background color of subview not working
我有一個非常簡單的UIViewController,其主視圖具有以下結構:
UIView
-->CustomView
我的CustomView看起來像這樣
-(id) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setBackgroundColor:[UIColor blueColor]];
NSLog([NSString stringWithFormat:@"Init with frame %f,%f,%f,%f", self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height]);
}
return self;
}
- (void)drawRect:(CGRect)rect {
NSLog([NSString stringWithFormat:@"Drawing CGRect %f,%f,%f,%f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height]);
[self setBackgroundColor:[UIColor yellowColor]];
}
在日志中,我可以看到在適當的框架下調用了init
,但從未調用過我的drawRect
方法,並且第一個 backgroundColor
(藍色的)更改甚至都無效。
如果將頂視圖(上圖中的UIView)設置為CustomView
,那么背景的確變為藍色,並且調用drawRect
; 背景不會變黃。
我真的不明白發生了什么,我想感謝您的輸入。
似乎未顯示自定義視圖。 是否已將其作為子視圖正確添加到頂視圖? 您是否檢查了其超級視圖是否為頂視圖(UIView)?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.