简体   繁体   English

UIView子类不能从黑色背景改变

[英]UIView subclass can't change from black background

I've created a subview of UIView to make some drawings - the drawings work fine in the drawRect method of my subclass, however, I cannot change the background color of the view. 我已经创建了UIView的子视图来制作一些图纸 - 图纸在我的子类的drawRect方法中工作正常,但是,我无法更改视图的背景颜色。 A little googling tells me I haven't set the frame for the view, but I'm not entirely sure how to do this. 一个小小的谷歌搜索告诉我,我没有为视图设置框架,但我不完全确定如何做到这一点。 I tried two things: 我尝试了两件事:

I create the view in Storyboard and add it to my view controller, then declare it as a property in the header file and link them up. 我在Storyboard中创建视图并将其添加到我的视图控制器,然后将其声明为头文件中的属性并将它们链接起来。 I synthesize the property at the top of the implementation file and in the viewDidLoad method, I add: 我在实现文件的顶部合成了属性,在viewDidLoad方法中,我添加:

[myView setBackgroundColor: [UIColor whiteColor]];

The view's background is still black. 视图的背景仍然是黑色的。

I also tried: 我也尝试过:

ViewSubclass *v = [[ViewSubclass alloc] initWithFrame: self.view.frame];
v.backgroundColor = [UIColor whiteColor];
myView = v;

To no avail. 无济于事。

What am I doing wrong? 我究竟做错了什么?

UPDATE: This is the code I use to draw in the view, in case there's something going on there! 更新:这是我用来在视图中绘制的代码,以防有什么事情发生!

- (void)drawRect:(CGRect)rect {
CGFloat height = self.bounds.size.height;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
CGFloat barWidth = 30;
int count = 0;
for (NSNumber *num in samples) {
    CGFloat x = count * (barWidth + 10);
    CGRect barRect = CGRectMake(x, height - ([num floatValue] * height), barWidth, [num floatValue] * height);
    CGContextAddRect(context, barRect);
    count++;
}
CGContextFillPath(context);
}

It just creates a set of bars in the screen, of different heights. 它只是在屏幕上创建一组不同高度的条形图。

CGContextClearRect From the docs: CGContextClearRect来自文档:

If the provided context is a window or bitmap context, Quartz effectively clears the rectangle. 如果提供的上下文是窗口或位图上下文,Quartz会有效地清除矩形。 For other context types, Quartz fills the rectangle in a device-dependent manner. 对于其他上下文类型,Quartz以与设备相关的方式填充矩形。 However, you should not use this function in contexts other than window or bitmap contexts. 但是,您不应在窗口或位图上下文以外的上下文中使用此函数。

It might be that this clearing clears your entire view with no regard to the background color you set 可能是这个清除清除了您的整个视图而不考虑您设置的背景颜色

I can t add comments due my low lvl, but the question springs to mind, have you added to your UIView to viewcontroller? 由于我的低lvl,我无法添加评论,但问题是脑海中浮现,你有没有添加到你的UIView查看控制器?

like 喜欢

[self.view addSubview:v];

EDIT: 编辑:

I'm sorry I had understood that the second option was added to the view that pragmatically. 对不起,我已经明白第二个选项被添加到了实用的视图中。

Are you adding the view in the header file? 您是否在头文件中添加了视图? If so you might need to be sure the frame's coordinates are visible and that the view is on top, also make the new view slightly smaller to see if it is actually being created: 如果是这样,您可能需要确保框架的坐标是可见的并且视图位于顶部,同时使新视图稍微小一点以查看它是否实际被创建:

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
[self.view addSubview: myView];
[self.view bringSubviewToFront:myView];

You can also change the main view background color to see if the new view is actually being created. 您还可以更改主视图背景颜色以查看是否实际创建了新视图。 For example: 例如:

[self.view setBackgroundColor:[UIColor grayColor]];

If the color is changing for your main view then the problem is that your new view is not being or brought to the front. 如果主视图的颜色发生变化,则问题在于您的新视图未被显示或未显示在前面。 Let us know how it goes! 让我们知道怎么回事!

At the point where do 在做的地方

[myView setBackgroundColor: [UIColor whiteColor]];

check to see if myView is not nil in the debugger 检查调试器中的myView是否为nil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM