ios 设置 UIVIew 的背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7562588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:38:37  来源:igfitidea点击:

Setting the background color of a UIVIew

iphoneobjective-ciosuiview

提问by WolfLink

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 backgroundColorproperty of your UIView. For example:

编辑:人们一直在访问这篇没有太多有用信息的帖子,所以我把它放在这里是为了帮助你们:尝试设置backgroundColor你的UIView. 例如:

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). If that doesn't work, something weird is going on. Keep looking, this post won't help you.

确保您的视图已被实例化,并且如果它完全由 xib 或故事板控制,请确保该视图已被加载(否则界面构建器中设置的任何内容都将成为背景颜色)。如果这不起作用,那么奇怪的事情正在发生。继续看,这篇文章对你没有帮助。

Original post:

原帖:

I have an NSObject with a UIView property. Mostly I use this UIView for displaying a picture, but in some cases, I want to set the background color of it. 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? Here is some of my code:

我有一个带有 UIView 属性的 NSObject。大多数情况下,我使用这个 UIView 来显示图片,但在某些情况下,我想设置它的背景颜色。但是,每次我尝试设置背景颜色时,颜色都会保持清晰。任何图像或其他子视图都会出现,但背景不会出现。如何更改 UIView 的背景颜色?这是我的一些代码:

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 添加到视图控制器的视图后调用的函数中)

(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.

在这两种情况下,背景都保持清晰。

采纳答案by WolfLink

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.

我不确定问题是什么,虽然我知道这不是你回答过的问题之一,但上次我去解决这个问题时,我注意到它正在工作。无论如何,谢谢你的帮助。

回答by Bourne

Are you allocating and initializing the UIView mentioned above? The problem should be that you have most likely forgotten to do that for the UIView called picture.

你在分配和初始化上面提到的 UIView 吗?问题应该是您很可能忘记为名为图片的 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。

回答by progrmr

alphahas a valid range of 0.0 to 1.0. You have it set to 50, try 1 instead:

alpha有效范围为 0.0 到 1.0。您已将其设置为 50,请改为尝试 1:

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

回答by Anand

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 UIColorto set the color for backgound

否则使用UIColor设置背景颜色

picture.backgroundColor = [UIColor greenColor];

回答by alaroldai

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 外观中。

回答by Chau

Can use attribute inspector for change color. Select color for 'Background'.

可以使用属性检查器来改变颜色。为“背景”选择颜色。

回答by Vitali Tchalov

Your code seems totally valid and it should change the background colour. You may want to double check that picture.opaqueproperty is set to YES(it's the default value unless your code change it). 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:

您的代码似乎完全有效,它应该更改背景颜色。您可能需要仔细检查该picture.opaque属性是否设置为YES(除非您的代码更改它,否则它是默认值)。但更可能的问题是视图的框架大小为零。我建议验证这一点。例如,通过记录它:

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

Cheers

干杯