简体   繁体   English

设置UIVIew的背景颜色

[英]Setting the background color of a UIVIew

EDIT: People keep visiting this post which doesn't have much good information so I'll put this here to help you guys: Try setting the backgroundColor property of your UIView . 编辑:人们一直在访问这篇文章,这篇文章没有太多好的信息,所以我会在这里帮助你们:尝试设置你的UIViewbackgroundColor属性。 For example: 例如:

myView.backgroundColor = [UIColor blueColor];

Make sure your view has been instantiated and if it is controlled by an xib or storyboard at all, make sure that the view has already been loaded (otherwise whatever is set in interface builder will become the background color). 确保您的视图已实例化,如果它完全由xib或storyboard控制,请确保已加载视图(否则在界面构建器中设置的任何内容将成为背景颜色)。 If that doesn't work, something weird is going on. 如果这不起作用,那就会发生一些奇怪的事情。 Keep looking, this post won't help you. 继续看,这篇文章对你没有帮助。

Original post: 原帖:

I have an NSObject with a UIView property. 我有一个带有UIView属性的NSObject。 Mostly I use this UIView for displaying a picture, but in some cases, I want to set the background color of it. 大多数情况下,我使用这个UIView来显示图片,但在某些情况下,我想设置它的背景颜色。 However, each time I try to set the background color, the color stays as clear. 但是,每次我尝试设置背景颜色时,颜色都保持清晰。 Any images or other subviews appear but the background does not. 出现任何图像或其他子视图,但背景不显示。 How can I change the background color of a UIView? 如何更改UIView的背景颜色? Here is some of my code: 这是我的一些代码:

here is an example where I need a tiled image: (this is in a function called after I add the UIView to the viewcontroller's view) 这里是一个我需要平铺图像的例子:(这是在我将UIView添加到viewcontroller视图后调用的函数中)

(picture is a UIView Property) (图片是UIView属性)

 picture.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Tile.png"]];

I have also tried in other cases: 我还试过其他情况:

picture.backgroundColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:0.5];

In both cases, the background remains clear. 在这两种情况下,背景仍然清晰。

Are you allocating and initializing the UIView mentioned above? 你在分配和初始化上面提到的UIView吗? The problem should be that you have most likely forgotten to do that for the UIView called picture. 问题应该是你很可能忘记为UIView调用图片。

Just try: 试一试:

picture = [[UIView alloc] initWithFrame:CGRectMake(10,10,200,200)];
picture.backgroundColor = [UIColor redColor];

Assuming that picture has already been declared an iVar of type UIView in your interface file. 假设图片已经在您的接口文件中声明为UIView类型的iVar。

alpha has a valid range of 0.0 to 1.0. alpha的有效范围为0.0到1.0。 You have it set to 50, try 1 instead: 你把它设置为50,尝试1:

picture.backgroundColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:1];

If you are good in using RGB value you can use 如果您擅长使用RGB值,则可以使用

picture.backgroundColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:1];

Otherwise use UIColor to set the color for backgound 否则使用UIColor设置背景的颜色

picture.backgroundColor = [UIColor greenColor];

It's probably worth checking whether you're changing the background color on the main thread or not. 可能值得检查您是否正在更改主线程上的背景颜色。 If you change the background color in a dispatch queue (other than the main queue) it can sometimes not be reflected in the UI appearance. 如果更改调度队列(主队列除外)中的背景颜色,有时可能无法在UI外观中反映出来。

Can use attribute inspector for change color. 可以使用属性检查器来更改颜色。 Select color for 'Background'. 选择“背景”的颜色。

I'm not sure what the problem was, although I do know it was not one of the problems you have answered with, but last time I went to work on the problem I noticed that it was working. 我不确定问题是什么,虽然我知道这不是你回答的问题之一,但是上次我去研究这个问题我注意到它有效。 Thank you for your help anyway. 无论如何,谢谢你的帮助。

Your code seems totally valid and it should change the background colour. 您的代码似乎完全有效,它应该更改背景颜色。 You may want to double check that picture.opaque property is set to YES (it's the default value unless your code change it). 您可能需要仔细检查picture.opaque属性是否设置为YES (除非您的代码更改它,否则它是默认值)。 But a more likely problem is that the frame of the view has zero size. 但更可能的问题是视图的框架大小为零。 I would advise to verify this. 我建议验证这一点。 For example, by logging it: 例如,通过记录它:

    NSLog(@"Frame size: width=%f, height=%f", picture.frame.size.width, picture.frame.size.height);

Cheers 干杯

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

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